Skip to content

[BUG] GM_addElement加载外部脚本失败 #728

Closed

Description

@n1nja88888

问题描述

我的脚本需要引入一个外部js资源,所以用的GM_addElement API来做,之前在Violent Monkey插件中是可以正常运行的,但是在Script Cat就不行了。感觉应该是二者对这个API实现不同是吗?

具体报错如下:
[GM_addElement](content.js:1 Refused to load the script 'https://g.alicdn.com/AWSC/AWSC/awsc.js' because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' chrome-extension://ba9dd7ac-6bbe-473c-a0bf-34f42c0d1a73/". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.)

引入的资源是https://g.alicdn.com/AWSC/AWSC/awsc.js,不知道是不是这个js代码中还会通过创建script标签来继续引入js资源导致报错,但是之前在Violent Monkey是没有这个问题的。

重现步骤

脚本中添加如下代码:

GM_addElement('script', { src: 'https://g.alicdn.com/AWSC/AWSC/awsc.js' })

脚本猫版本

1.1.1

操作系统以及浏览器信息

Windows 11 Version 140.0.7339.81 (Official Build) (64-bit)

补充信息 (选填)

No response

Activity

  1. CodFrm commented on Sep 12, 2025

    @CodFrm
    Member

    运行在哪个网站上呢?

  2. n1nja88888 commented on Sep 12, 2025

    @n1nja88888
    Author

    任何网站吧,我的这个脚本匹配所有网站:

    // @match         *://*/*
    
  3. CodFrm commented on Sep 12, 2025

    @CodFrm
    Member

    这个脚本没有复现,我意思是在哪个网站上会复现,应该是和网站csp策略有关系

    // ==UserScript==
    // @name         New Userscript K2RI-1
    // @namespace    https://docs.scriptcat.org/
    // @version      0.1.0
    // @description  try to take over the world!
    // @author       You
    // @match        https://bbs.tampermonkey.net.cn/
    // @icon         https://www.google.com/s2/favicons?sz=64&domain=docs.scriptcat.org
    // @grant        GM_addElement
    // ==/UserScript==
    
    (function () {
        'use strict';
        const el = GM_addElement('script', { src: 'https://g.alicdn.com/AWSC/AWSC/awsc.js' })
        console.log(el);
        // Your code here...
    })();
  4. n1nja88888 commented on Sep 12, 2025

    @n1nja88888
    Author

    我尝试了你的代码,还是会有这个问题:

    Image

    是不是我的Chrome还是Script Cat插件配置错误了什么东西?

  5. CodFrm commented on Sep 12, 2025

    @CodFrm
    Member

    复现了,感谢反馈,我看看什么问题

  6. CodFrm commented on Sep 12, 2025

    @CodFrm
    Member

    感谢反馈,在最新的分支中已经修复:https://github.com/scriptscat/scriptcat/actions/runs/17641060731

    应该是1.1.1版本使用的 chrome.scripting.RegisteredContentScript 注入的content脚本,chrome.userScripts.configureWorld 的csp策略无法应用导致的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions