找回密码
 立即注册
首页 业界区 安全 GKD快照审查的network-extension网络扩展在scriptcat上 ...

GKD快照审查的network-extension网络扩展在scriptcat上重构

百杲憔 3 天前
network-extension:Github
ScriptCat:v0.17.0-beta.1
  1. // ==UserScript==
  2. // @name               network-extension
  3. // @name:zh-CN         网络扩展-ScriptCat
  4. // @namespace          https://github.com/lisonge
  5. // @version            1.0.7
  6. // @author             lisonge
  7. // @description        Inject GM_XHR to Website
  8. // @description:zh-CN  注入GM_XHR到网站
  9. // @icon               https://vitejs.dev/logo.svg
  10. // @homepageURL        https://github.com/gkd-kit/network-extension
  11. // @homepage           https://github.com/gkd-kit/network-extension
  12. // @match              http://i.gkd.li/snapshot/*
  13. // @match              https://i.gkd.li/snapshot/*
  14. // @connect            *
  15. // @grant              GM.getValue
  16. // @grant              GM.registerMenuCommand
  17. // @grant              GM.setValue
  18. // @grant              GM.xmlHttpRequest
  19. // @grant              GM.unregisterMenuCommand
  20. // @grant              GM_log
  21. // @grant              unsafeWindow
  22. // @noframes
  23. // @antifeature        This script exposes GM.xmlHttpRequest to the page, which may have security implications
  24. // ==/UserScript==
  25. (function() {
  26.     'use strict';
  27.    
  28.     // 配置检查
  29.     const CONFIG_KEY = `inject_network:${location.origin}`;
  30.     const MENU_NAME = {
  31.         'zh-CN': '注入GM_XHR到当前网站',
  32.         'default': 'Inject GM_XHR to Website'
  33.     };
  34.    
  35.     // 获取本地化菜单名称
  36.     function getMenuName() {
  37.         // 优先匹配完整语言代码
  38.         if (MENU_NAME[navigator.language]) {
  39.             return MENU_NAME[navigator.language];
  40.         }
  41.         
  42.         // 尝试匹配主要语言(如 zh)
  43.         const primaryLang = navigator.language.split('-')[0];
  44.         for (const lang in MENU_NAME) {
  45.             if (lang.split('-')[0] === primaryLang) {
  46.                 return MENU_NAME[lang];
  47.             }
  48.         }
  49.         
  50.         return MENU_NAME.default;
  51.     }
  52.    
  53.     // 创建菜单控制器
  54.     async function createMenuController() {
  55.         const menuName = getMenuName();
  56.         let isEnabled = await GM.getValue(CONFIG_KEY, false);
  57.         let menuId;
  58.         
  59.         // 注册菜单命令
  60.         function registerMenu() {
  61.             const icon = isEnabled ? '✅' : '❎';
  62.             menuId = GM.registerMenuCommand(
  63.                 `${icon} ${menuName}`,
  64.                 toggleInjection
  65.             );
  66.         }
  67.         
  68.         // 切换注入状态
  69.         async function toggleInjection() {
  70.             isEnabled = !isEnabled;
  71.             await GM.setValue(CONFIG_KEY, isEnabled);
  72.             
  73.             // 显示状态变更通知
  74.             const status = isEnabled ? '已启用' : '已禁用';
  75.             GM_log(`网络扩展 ${status},页面将刷新...`, 'info');
  76.             
  77.             // 刷新页面使更改生效
  78.             setTimeout(() => location.reload(), 300);
  79.         }
  80.         
  81.         // 初始化菜单
  82.         registerMenu();
  83.         
  84.         return {
  85.             isEnabled: () => isEnabled,
  86.             updateMenu: function() {
  87.                 GM.unregisterMenuCommand(menuId);
  88.                 registerMenu();
  89.             }
  90.         };
  91.     }
  92.    
  93.     // 主执行函数
  94.     async function main() {
  95.         const menu = await createMenuController();
  96.         
  97.         if (menu.isEnabled()) {
  98.             // 暴露 API 到页面全局对象
  99.             unsafeWindow.__NetworkExtension__ = {
  100.                 GM_xmlhttpRequest: GM.xmlHttpRequest,
  101.                 GM: {
  102.                     xmlHttpRequest: GM.xmlHttpRequest
  103.                 }
  104.             };
  105.             
  106.             // 安全警告日志
  107.             GM_log(
  108.                 `已向页面暴露 GM.xmlHttpRequest API,当前域: ${location.origin}`,
  109.                 'warn'
  110.             );
  111.             
  112.             // 添加控制台警告
  113.             console.warn('[网络扩展] GM.xmlHttpRequest API 已暴露给页面,仅限受信任网站使用');
  114.         }
  115.     }
  116.    
  117.     // 启动脚本
  118.     main()
  119.         .then(() => {
  120.             GM_log('网络扩展初始化完成', 'debug');
  121.         })
  122.         .catch(error => {
  123.             GM_log(`脚本初始化失败: ${error.message}`, 'error');
  124.             console.error('网络扩展初始化错误:', error);
  125.         });
  126. })();
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册