GKD网页审查工具规则复制增强

在GKD网页审查工具复制规则后,弹出菜单,让用户自定义部分内容

目前為 2024-03-05 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GKD网页审查工具规则复制增强
// @namespace    https://blog.adproqwq.xyz
// @version      1.3.1
// @description  在GKD网页审查工具复制规则后,弹出菜单,让用户自定义部分内容
// @author       Adpro
// @match        https://i.gkd.li/snapshot/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';
  let layuiScript = document.createElement('script');
  layuiScript.src = 'https://www.layuicdn.com/layui/layui.js';
  let layuiCss = document.createElement('link');
  layuiCss.rel = 'stylesheet';
  layuiCss.href = 'https://www.layuicdn.com/layui/css/layui.css';
  document.body.appendChild(layuiScript);
  document.head.appendChild(layuiCss);
  if(window.localStorage.getItem('categories') === null) window.localStorage.setItem('categories','');

  let app = document.getElementById('app');
  let config = {
    childList: true
  };
  let callback = (mutationsList)=>{
    mutationsList.forEach(() => {
      searchClickFunc();
    });
  };
  
  let observer = new MutationObserver(callback);
  observer.observe(app,config);

  function searchClickFunc(){
    try {
      throw typeof document.getElementsByClassName('n-input-group')[0];
    } catch (error) {
      if(error == 'undefined') return;
    }
    observer.disconnect();
    let inputGroup = document.getElementsByClassName('n-input-group')[0];
    let searchButton = inputGroup.getElementsByClassName('n-button n-button--default-type n-button--medium-type')[0];
    searchButton.onclick = () => {
      let eachSearchResultActionBar = document.getElementsByClassName('n-collapse-item__header-extra');
      if(eachSearchResultActionBar.length != 0){
        for(let i = 0;i < eachSearchResultActionBar.length;i++){
          eachSearchResultActionBar[i].getElementsByClassName('n-button n-button--default-type n-button--small-type')[0].onclick = () => {
            modifyRule();
          };
        }
      }
    };
    document.getElementsByClassName('n-input__input-el gkd_code')[0].onkeyup = (e) => {
      if(e.keyCode == '13'){
        /*使用setTimeout解决eachSearchResultActionBar.length为0问题*/
        setTimeout(()=>{
          let eachSearchResultActionBar = document.getElementsByClassName('n-collapse-item__header-extra');
          if(eachSearchResultActionBar.length != 0){
            for(let i = 0;i < eachSearchResultActionBar.length;i++){
              eachSearchResultActionBar[i].getElementsByClassName('n-button n-button--default-type n-button--small-type')[0].onclick = () => {
                modifyRule();
              };
            }
          }
        },1);
      }
    };
  }

  var mode = '1', cate = '';

  function modifyRule(){
    var layer = layui.layer;
    var form = layui.form;
    var element = layui.element;
    navigator.clipboard.readText().then((a)=>{
      let rule = JSON5.parse(a);
      layer.open({
        type: 1,
        title: '菜单',
        area: 'auto',
        shadeClose: true,
        success: (layero, index, that) => {
          document.getElementsByName('copy')[0].onclick = ()=>{
            let rulePos = document.getElementsByName('Rpos')[0].value;
            let ruleName = document.getElementsByName('Rname')[0].value;
            let ruleDesc = document.getElementsByName('Rdesc')[0].value;

            if(rulePos != ''){
              if(rulePos.startsWith('{') == true && rulePos.endsWith('}') == true){
                let position = JSON5.parse(rulePos);
                let positionKeys = Object.keys(position);
                if(positionKeys.length == 2){
                  if(positionKeys.indexOf('left') != -1 && positionKeys.indexOf('right') != -1){
                    layer.msg('坐标非法',{icon: 2});
                    return;
                  }
                  else if(positionKeys.indexOf('top') != -1 && positionKeys.indexOf('bottom') != -1){
                    layer.msg('坐标非法',{icon: 2});
                    return;
                  }
                  else if(positionKeys[0] == positionKeys[1]){
                    layer.msg('坐标非法',{icon: 2});
                    return;
                  }
                  else{
                    rule.groups[0].rules[0].action = 'clickCenter';
                    rule.groups[0].rules[0].position = position;
                  }
                }
                else{
                  layer.msg('坐标非法',{icon: 2});
                  return;
                }
              }
              else{
                layer.msg('你填写的坐标不是一个对象',{icon: 2});
                return;
              }
            }

            if(ruleName != '') rule.groups[0].name = cate + '-' + ruleName;
            else{
              if(cate != '') rule.groups[0].name = cate;
            }

            if(ruleDesc == '') delete rule.groups[0].desc;
            else rule.groups[0].desc = ruleDesc;

            copyRule(rule, mode);

            layer.close(index);
          };

          document.getElementsByName('saveSettings')[0].onclick = ()=>{
            let categories = document.getElementsByName('categories')[0].value;
            window.localStorage.setItem('categories',categories);
            layer.msg('保存成功',{icon: 6});
            categories();
          };

          categories();
        },
        content: `
          <div class="layui-tab layui-tab-brief" lay-filter="tab-filter">
            <ul class="layui-tab-title">
              <li class="layui-this" lay-id="info">信息配置</li>
              <li lay-id="settings">设置</li>
            </ul>
            <div class="layui-tab-content">
              <div class="layui-tab-item layui-show">
                <div class="layui-form">
                  <div class="layui-form-item">
                    <label class="layui-form-label">选择模式:</label>
                    <input type="radio" name="mode" value="0" title="ts模式" lay-filter="mode-filter">
                    <input type="radio" name="mode" value="1" title="app模式" lay-filter="mode-filter" checked>
                    <input type="radio" name="mode" value="2" title="groups模式" lay-filter="mode-filter">
                    <input type="radio" name="mode" value="3" title="rules模式" lay-filter="mode-filter">
                  </div>
                  <div id="categories" class="layui-form-item">
                    <label class="layui-form-label">选择分类:</label>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">修改key为:</label>
                    <div class="layui-input-group">
                      <input type="number" name="Rkey" lay-affix="ok" lay-filter="ok" class="layui-input">
                      <div class="layui-input-suffix">
                        <i class="layui-icon layui-icon-tips"></i> ts模式、app模式和groups模式修改groups内的key;rules模式修改rules内的key。记得点勾!
                      </div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">坐标:</label>
                    <div class="layui-input-block">
                      <input type="text" name="Rpos" placeholder="一个合法的坐标对象,若无不填" autocomplete="off" lay-affix="clear" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">规则组名称:</label>
                    <div class="layui-input-block">
                      <input type="text" name="Rname" placeholder="${rule.groups[0].name}" autocomplete="off" lay-affix="clear" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">规则组描述:</label>
                    <div class="layui-input-block">
                      <input type="text" name="Rdesc" placeholder="没有描述不填" autocomplete="off" lay-affix="clear" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-btn-container">
                    <button type="button" name="copy" class="layui-btn">确定</button>
                  </div>
                </div>
              </div>
              <div class="layui-tab-item">
                <div class="layui-form">
                  <div class="layui-form-item">
                    <label class="layui-form-label">分类:</label>
                    <div class="layui-input-block">
                      <textarea name="categories" cols=50 rows=15 placeholder="多个{key, name}组成的数组,支持JSON5语法" class="layui-textarea"></textarea>
                    </div>
                  </div>
                  <div class="layui-btn-container">
                    <button type="button" name="saveSettings" class="layui-btn">保存</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        `,
      });
      layui.use(()=>{
        form.render();
        element.render('tab', 'tab-filter');
      });
      form.on('radio(mode-filter)', (data)=>{
        let elem = data.elem;
        mode = elem.value;
      });
      form.on('radio(cate-filter)', (data)=>{
        let elem = data.elem;
        cate = elem.value;
      });
      form.on('input-affix(ok)', function(data){
        let elem = data.elem;
        let value = elem.value;
        if(value != ''){
          if(mode == '0' || mode == '1' || mode == '2') rule.groups[0].key = Number(value);
          else rule.groups[0].rules[0].key = Number(value);
          layer.msg('修改成功',{icon: 1});
        }
      });
    });
  };

  function copyRule(rule, mode){
    if(mode == '0'){
      const tsFile = `import { defineAppConfig } from '../types';\r\rexport default defineAppConfig(${JSON5.stringify(rule,null,2)});\r`;
      navigator.clipboard.writeText(tsFile).then(()=>{
        layer.msg('注入修改成功',{icon: 1});
      });
    }
    else if(mode == '1'){
      navigator.clipboard.writeText(JSON5.stringify(rule,null,2)).then(()=>{
        layer.msg('注入修改成功',{icon: 1});
      });
    }
    else if(mode == '2'){
      navigator.clipboard.writeText(JSON5.stringify(rule.groups[0],null,2)).then(()=>{
        layer.msg('注入修改成功',{icon: 1});
      });
    }
    else if(mode == '3'){
      navigator.clipboard.writeText(JSON5.stringify(rule.groups[0].rules[0],null,2)).then(()=>{
        layer.msg('注入修改成功',{icon: 1});
      });
    }
  }

  function categories(){
    let categories = window.localStorage.getItem('categories');
    let CateChoo = document.getElementById('categories');
    if(categories != ''){
      categories = JSON5.parse(categories);
      categories.forEach((b)=>{
        let TInput = document.createElement('input');
        TInput.type = 'radio';
        TInput.name = 'RCate';
        TInput.value = b.name;
        TInput.title = b.name;
        TInput.setAttribute('lay-filter','cate-filter');
        CateChoo.appendChild(TInput);
      });
    }
  }

})();