【问题标题】:How to make a ViolentMokey userScript into a Browser Extension?如何将 ViolentMokey 用户脚本制作成浏览器扩展?
【发布时间】:2020-07-26 00:26:53
【问题描述】:

我一直在尝试将用户脚本迁移到我自己的扩展中,但由于某种原因我无法运行以下代码:

// ==/UserScript==
console.info('BEFORE Hooked! MONKEY');
(function() {
    'use strict';
    // Reference [Augular loaded detect]: https://stackoverflow.com/a/31970556/9182265
    var initWatcher = setInterval(function () {
        if (window.MegaUtils) {
            console.info(window.MegaUtils);
            clearInterval(initWatcher);
            hookImport();
            hookFull();
            console.info('FUNtions Hooked! MONKEY');
        }
    }, 500);
})();

但由于某种原因,IF 语句永远不会为 TRUE,而是从 ViolentMonkey 运行完全相同的代码,它可以立即运行。

所以 window.MegaUtils 根本没有被检测到,我不知道为什么。 有人告诉我,我的扩展程序可能无法访问 DOM 对象,但为什么 ViolentMonkey 确实可以访问它。

这是我在 Chrome 中导入扩展程序时使用的清单:

{
    "manifest_version": 2,
    "content_scripts": [ {
        "exclude_globs":    [  ],
        "include_globs":    [ "*" ],
        "js":               [ "mega.user.js" ],
        "matches":          [   "https://mega.nz/*",
                                "http://mega.nz/*" ],
        "run_at": "document_end"
    } ],
    "converted_from_user_script": true,
    "description":  "testing extension",
    "permissions": [],
    "name":         "MegaByPass",
    "icons": {
        "16": "images/mega-cloud-icon.png",
        "32": "images/mega-cloud-icon.png",
        "48": "images/download.png",
        "128": "images/873133.png"
      },
    "version":      "1.0"
}

source

提前致谢。

【问题讨论】:

  • 上面的代码在哪里运行?背景脚本还是内容脚本?它是如何注入的?
  • 我相信这就是您要问的://@run-at document-end full source in:greasyfork.org/en/scripts/397876-mega-nz-ultimately-import/code
  • 没有。我在问adon如何注入上述脚本?通过manifest.jsontabs.executeScriptcontentScripts.register()userScripts.register() 等?
  • 这是一个很好的问题,我正在使用 manifest.json 但不确定 ViolentMonkey 是如何做到的。如何找出来?我将清单添加到问题中。
  • 回复更新了更多信息。

标签: javascript greasemonkey userscripts


【解决方案1】:

首先,删除 glob,因为它可能会导致问题。

"content_scripts": [
  {
    "matches": ["http://mega.nz/*", "https://mega.nz/*"],
    "js": ["mega.user.js"]
  }
]

ViolentMonkey 默认注入document-end。但是,GM/VM/TM 手动注入用户脚本而不使用专用 API(Firefox 中的 FireMonkey 使用专用 API),因此注入时间可能比浏览器 API 注入时晚。

尝试使用默认的"document_idle"(您可以不使用它)。

使用"document_end" 可能会导致脚本在外部 Angualr 加载之前运行,这可能是问题的原因。

为了进行正确的测试,需要实际的扩展。

更新 1

内容脚本被注入到与其所在页面不同的范围/上下文中。因此它们不能直接与页面上的 JS 交互,反之亦然。

不同浏览器之间的全局window 行为并不统一(例如,chrome 中的eval() 始终在内容脚本的上下文中运行,但在Firefox 中eval() 在内容范围内运行,而window.eval() 在页面范围内运行)。

经过快速测试,内容脚本无法访问全局windowwindow.MegaUtils。有一些方法可以解决这个问题,但用户脚本工作的原因可能与 ViolentMonkey 注入它的方式或在不使用 unsafewindow 的情况下授予对 window 对象的访问权限有关。

您是否使用任何其他脚本管理器测试过该脚本?!!该脚本适用于所有脚本管理器还是仅适用于 ViolentMonkey?

更多信息:
Accessing all the window variables of the current tab in chrome extension
Insert code into the page context using a content script

PS。我只在 Firefox 上测试,因为我不使用 Chrome。

更新 2

查看Can't find page variables when used GM_ functions,当有@grant none 时,GM|TM|VM 似乎正在将用户脚本注入页面内容(需要适当的确认)。这可以解释为什么上面带有@grant none 的用户脚本可以工作并且可以在GM|TM|VM(不是FM)中获得window.MegaUtils。在这种情况下,您需要在页面 JS 中注入脚本。

这是一个例子:

const script = document.createElement('script');
script.textContent = `(function() {
    'use strict';
    // Reference [Augular loaded detect]: https://stackoverflow.com/a/31970556/9182265
    var initWatcher = setInterval(function () {
        if (window.MegaUtils) {
            clearInterval(initWatcher);
            hookImport();
            hookFull();
            console.info('FUNtions Hooked!');
        }
    }, 500);
})();

....`;
document.body.appendChild(script);

更新 3 CSP

目前,浏览器遵守页面 CSP(内容安全策略),这是您在评论中提到的问题。
参考:
[meta] Page CSP should not apply to content inserted by content scripts (V2 issue)
CSP 'sandbox' directive prevents content scripts from matching, due to unique origin, breaking also browser features [Screenshots]

有一些方法可以解决这个问题,但它们不是标准的,扩展不应绕过浏览器或页面 CSP。

【讨论】:

  • Chrome 会抱怨删除匹配项和 js 字段,所以他们必须在那里才能导入它。我尝试使用 document_idle,但同样的行为它永远不会检测到 window.MegaUtils。
  • @ChopLabalagun 为了正确测试,需要实际的扩展。
  • 你可以从这里得到它:drive.google.com/drive/folders/…
  • @ChopLabalagun 答案已更新.. 虽然问题没有解决。
  • 感谢您的尝试,这很奇怪。我期待一个非常直接的集成。
【解决方案2】:

我很确定 user_script 而不是 content_script 是要走的路。

此 API 提供与 contentScripts 类似的功能,但具有适合处理第三方脚本的功能:

  • 访问窗口并记录与用户脚本附加到的网页相关的全局值。

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts

虽然它的工作方式有点不同,但仍在尝试理解它:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2013-10-03
    • 2019-11-05
    • 2022-06-10
    • 2016-05-12
    相关资源
    最近更新 更多