copy LaTeX from zhihu

2022/9/10 01:28:03

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        copy LaTeX from zhihu
// @namespace   Violentmonkey Scripts
// @match       https://zhuanlan.zhihu.com/*
// @grant       none
// @version     1.0
// @author      chopong
// @license MIT
// @description 2022/9/10 01:28:03
// ==/UserScript==



(function(){
  "use strict";
  var mathblocks = document.querySelectorAll(".ztext-math");
  var num = mathblocks.length;
  if (num>0){
    for (var i=0;i<num;i++){
      mathblocks[i].addEventListener("click",function(){
        let transfer = document.createElement('input');
        this.parentNode.insertBefore(transfer,this);
        transfer.value =  this.attributes['data-tex'].value ;
        // 这里表示想要复制的内容
        transfer.focus();
        transfer.select();
        if (document.execCommand('copy')) {
          document.execCommand('copy');
        }
        this.parentNode.removeChild(transfer);
        if(window.Notification && Notification.permission !== "denied") {
          Notification.requestPermission(function(status) {
            var n = new Notification('LaTeX', { body: transfer.value }); 
          });
        }
      });
    }
  }
})();