network-extension:Github
ScriptCat:v0.17.0-beta.1- // ==UserScript==
- // @name network-extension
- // @name:zh-CN 网络扩展-ScriptCat
- // @namespace https://github.com/lisonge
- // @version 1.0.7
- // @author lisonge
- // @description Inject GM_XHR to Website
- // @description:zh-CN 注入GM_XHR到网站
- // @icon https://vitejs.dev/logo.svg
- // @homepageURL https://github.com/gkd-kit/network-extension
- // @homepage https://github.com/gkd-kit/network-extension
- // @match http://i.gkd.li/snapshot/*
- // @match https://i.gkd.li/snapshot/*
- // @connect *
- // @grant GM.getValue
- // @grant GM.registerMenuCommand
- // @grant GM.setValue
- // @grant GM.xmlHttpRequest
- // @grant GM.unregisterMenuCommand
- // @grant GM_log
- // @grant unsafeWindow
- // @noframes
- // @antifeature This script exposes GM.xmlHttpRequest to the page, which may have security implications
- // ==/UserScript==
- (function() {
- 'use strict';
-
- // 配置检查
- const CONFIG_KEY = `inject_network:${location.origin}`;
- const MENU_NAME = {
- 'zh-CN': '注入GM_XHR到当前网站',
- 'default': 'Inject GM_XHR to Website'
- };
-
- // 获取本地化菜单名称
- function getMenuName() {
- // 优先匹配完整语言代码
- if (MENU_NAME[navigator.language]) {
- return MENU_NAME[navigator.language];
- }
-
- // 尝试匹配主要语言(如 zh)
- const primaryLang = navigator.language.split('-')[0];
- for (const lang in MENU_NAME) {
- if (lang.split('-')[0] === primaryLang) {
- return MENU_NAME[lang];
- }
- }
-
- return MENU_NAME.default;
- }
-
- // 创建菜单控制器
- async function createMenuController() {
- const menuName = getMenuName();
- let isEnabled = await GM.getValue(CONFIG_KEY, false);
- let menuId;
-
- // 注册菜单命令
- function registerMenu() {
- const icon = isEnabled ? '✅' : '❎';
- menuId = GM.registerMenuCommand(
- `${icon} ${menuName}`,
- toggleInjection
- );
- }
-
- // 切换注入状态
- async function toggleInjection() {
- isEnabled = !isEnabled;
- await GM.setValue(CONFIG_KEY, isEnabled);
-
- // 显示状态变更通知
- const status = isEnabled ? '已启用' : '已禁用';
- GM_log(`网络扩展 ${status},页面将刷新...`, 'info');
-
- // 刷新页面使更改生效
- setTimeout(() => location.reload(), 300);
- }
-
- // 初始化菜单
- registerMenu();
-
- return {
- isEnabled: () => isEnabled,
- updateMenu: function() {
- GM.unregisterMenuCommand(menuId);
- registerMenu();
- }
- };
- }
-
- // 主执行函数
- async function main() {
- const menu = await createMenuController();
-
- if (menu.isEnabled()) {
- // 暴露 API 到页面全局对象
- unsafeWindow.__NetworkExtension__ = {
- GM_xmlhttpRequest: GM.xmlHttpRequest,
- GM: {
- xmlHttpRequest: GM.xmlHttpRequest
- }
- };
-
- // 安全警告日志
- GM_log(
- `已向页面暴露 GM.xmlHttpRequest API,当前域: ${location.origin}`,
- 'warn'
- );
-
- // 添加控制台警告
- console.warn('[网络扩展] GM.xmlHttpRequest API 已暴露给页面,仅限受信任网站使用');
- }
- }
-
- // 启动脚本
- main()
- .then(() => {
- GM_log('网络扩展初始化完成', 'debug');
- })
- .catch(error => {
- GM_log(`脚本初始化失败: ${error.message}`, 'error');
- console.error('网络扩展初始化错误:', error);
- });
- })();
复制代码 来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |