You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TamperMonkey/Chrome中@connect标签失效,请求资源遭拒绝如何解决?

Tampermonkey @connect 指令无效仍报错的解决办法

问题重现

已在Tampermonkey脚本头部添加@connect example.org,脚本头部代码如下:

// ==UserScript==
// @name         My Userscript
// @namespace    com.example.my-userscript
// @version      0.2.14
// @description  Do things
// @author       Doktor J
// @match        https://example.com/index.php?id=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=example.com
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
// @require      https://example.org/path/jquery-csv.min.js
// @license      Mozilla Public License 2.0
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @connect      example.org
// ==/UserScript==

调用GM_xmlhttpRequest请求目标资源时:

GM_xmlhttpRequest({
    method: "GET",
    url: "https://example.org/path/data.csv",
    onload: function (response) {
        console.log(response.status);
        myData = $.csv.toObjects(response.responseText);
    }
});

仍弹出错误:

VM122:10 injected: Refused to connect to "https://example.org/path/data.csv": URL is not permitted

排查与解决步骤

  • 先刷新/重装脚本
    修改@connect指令后,必须在Tampermonkey里点击脚本的「重新安装」,或者刷新当前页面让新配置生效——这是最容易疏忽的点。
  • 检查@connect的写法
    当前@connect example.org格式没问题,但可以尝试添加通配符@connect *.example.org,覆盖可能存在的子域名访问场景。
  • 确认Tampermonkey的权限
    进入浏览器扩展管理页面,找到Tampermonkey,确保它拥有「访问所有网站数据」的权限,避免权限限制导致跨域请求失败。
  • 临时用通配符测试
    把@connect example.org替换成@connect *,如果请求成功,说明是域名匹配规则的问题,再改回精准域名;如果仍报错,检查Tampermonkey设置里是否开启了「安全模式」,该模式会限制跨域请求。
  • 检查目标URL是否重定向
    直接在浏览器打开https://example.org/path/data.csv,观察是否会跳转到其他域名。如果存在重定向,需要将跳转后的域名也加入@connect列表。
  • 确认脚本已在当前页面激活
    检查当前页面URL是否完全匹配@match https://example.com/index.php?id=*的规则,比如是否混淆了http/https协议、路径是否符合要求——脚本未激活的话,@connect规则不会生效。

内容的提问来源于stack exchange,提问作者Doktor J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 01:47:20