anti-select-none - csdn.net

解除 CSDN code block 的 select 鎖定

Tính đến 19-01-2022. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        anti-select-none - csdn.net
// @namespace   Violentmonkey Scripts
// @match       https://blog.csdn.net/*
// @grant       none
// @version     1.0.0
// @author      Lofairy
// @icon        https://blog.csdn.net/favicon.ico
// @description 解除 CSDN code block 的 select 鎖定
// ==/UserScript==

/* jshint esversion:6 */

(function() {
    'use strict';
    // console.info('init');

    var observer = new MutationObserver(resetTimer);
    var timer = setTimeout(action, 3000, observer);
    observer.observe(document, {childList: true, subtree: true});

    // reset timer every time something changes
    function resetTimer(changes, observer) {
        // console.info('timer');
        clearTimeout(timer);
        timer = setTimeout(action, 3000, observer);
    }

    function action(observer) {
      let codeElemants = document.querySelectorAll('pre[name=code]');
      if (codeElemants.length > 0) {
        observer.disconnect();
        // console.info('done');
        codeElemants.forEach(ele => {
            ele.children[0].style.userSelect = 'text';
            ele.children[0].style.webkitUserSelect = 'text';
            ele.children[1].remove();
        });
      }
    }
  
})();