// ==UserScript== // @name AnkiTool // @namespace https://ankiuser.net/edit/ // @version 0.2 // @description try to take over the world! // @author You // @match https://ankiuser.net/edit/ // @icon https://www.google.com/s2/favicons?sz=64&domain=ankiweb.net // @grant none // @run-at context-menu // ==/UserScript== (function() { 'use strict'; // Your code here... // 返回空闲的序号 function getIndex(text, index){ for(var i = index; i < 100; i++){ // 从小到大查找序号 if (text.indexOf("{{c" + i.toString()) < 0){ // 如果没有查到,返回空闲的序号 return i; } } } var path = "body > main > form > div:nth-child(3) > div > div"; if(window.location.href.length > 30){ //根据窗口链接长度判断是否是修改页面 path = "body > main > form > div:nth-child(1) > div > div"; } //获取选择的文本 var selecter = window.getSelection(); if (selecter.toString()){ // 如果选中文本则给选中的文本增加标记 var rang = selecter.getRangeAt(0); var select = selecter.toString(); var text = document.querySelector(path).innerHTML; rang.deleteContents(); // 删除选中内容 rang.insertNode(document.createTextNode("{{c" + getIndex(text, 1).toString() + "::" + select + "}}")); }else{ // 如果没有选中文本,则给所有的数字增加标记 var text1 = document.querySelector(path).innerText; var start = 1; document.querySelector(path).innerHTML = text1.replace(/(-?\d+)\.?(\d*)(?!::|年|月|日|\d|\.|}})/g, function (){ var index = getIndex(text1, start); start = index + 1; return "{{c" + index.toString() + "::" + arguments[0] + "}}" }); } })();