我一直在尝试将 userScript 迁移到我自己的扩展程序中,但由于某种原因我无法运行以下代码:
// ==/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 中导入扩展程序时使用的 list :
{
"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"
}
提前致谢。
最佳答案
首先,删除 glob,因为它可能会导致问题。
"content_scripts": [
{
"matches": ["http://mega.nz/*", "https://mega.nz/*"],
"js": ["mega.user.js"]
}
]
ViolentMonkey 默认在 document-end
注入(inject).然而,GM/VM/TM 是手动注入(inject)用户脚本,而不是使用专用 API(FireMonkey 在 Firefox 中使用专用 API),因此注入(inject)时间可能比浏览器 API 注入(inject)时间晚。
试试 "document_idle"
这是默认值(您可以忽略它)。
使用 "document_end"
可能导致脚本在加载外部 Angualr 之前运行,这可能是问题的原因。
为了进行正确的测试,需要实际的扩展。
更新 1
内容脚本被注入(inject)到与其所在页面不同的范围/上下文中。因此它们不能直接与页面上的 JS 交互,反之亦然。
全局window
不同浏览器之间的行为不统一(例如 eval()
在 chrome 中始终在内容脚本的上下文中运行,但在 Firefox 中 eval()
在内容范围内运行,但 window.eval()
在页面范围内运行)。
经过快速测试,内容脚本无法访问全局 window
& window.MegaUtils
.有一些方法可以解决这个问题,但用户脚本起作用的原因可能与 ViolentMonkey 注入(inject)它或授予对 window
的访问权的方式有关。不使用 unsafewindow
的对象.
您是否使用其他脚本管理器测试过该脚本?!!该脚本适用于所有脚本管理器还是仅适用于 ViolentMonkey?
更多信息:
Accessing all the window variables of the current tab in chrome extension
Insert code into the page context using a content script
附言。我只在 Firefox 上测试过,因为我不使用 Chrome。
更新 2
查看Can't find page variables when used GM_ functions , 看起来 GM|TM|VM 可能在有 @grant none
时将用户脚本注入(inject)页面内容(需要适当的确认)。这可以解释为什么上面的用户脚本带有 @grant none
可以得到 window.MegaUtils
在 GM|TM|VM(不是 FM)中。在这种情况下,您需要在页面 JS 中注入(inject)脚本。
这是一个例子:
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。
关于javascript - 如何将 ViolentMokey userScript 制作成浏览器扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63094905/