簡體   English   中英

如何將 ViolentMokey 用戶腳本制作成瀏覽器擴展?

[英]How to make a ViolentMokey userScript into a Browser Extension?

我一直在嘗試將 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 object,但為什么 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" }

資源

提前致謝。

首先,刪除 glob,因為它可能會導致問題。

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

ViolentMonkey 默認在document-end注入。 但是,GM/VM/TM 手動注入用戶腳本,而不是使用專用的 API(Firefox 中的 FireMonkey 使用專用 API),因此注入時間可能會晚於瀏覽器 ZDB974238714CA8DE644A7CE1DZ083 注入的時間。

嘗試使用默認的"document_idle" (您可以將其省略)。

使用"document_end"可能會導致腳本在外部 Angualr 加載之前運行,這可能是問題的原因。

為了進行正確的測試,需要實際的擴展。

更新 1

內容腳本被注入到與其所在頁面不同的范圍/上下文中。因此它們不能直接與頁面上的 JS 交互,反之亦然。

The global window behaviour is not uniform between different browsers (eg eval() in chrome always run in the context of the content script but in Firefox eval() runs in content scope but window.eval() in page scope).

經過快速測試,內容腳本無法訪問全局windowwindow.MegaUtils 有一些方法可以解決這個問題,但用戶腳本工作的原因可能與 ViolentMonkey 注入它的方式有關,或者在不使用unsafewindow的情況下授予對window object 的訪問權限。

您是否使用任何其他腳本管理器測試過該腳本??! 該腳本是否適用於所有腳本管理器或僅適用於 ViolentMonkey?

更多信息:
在 chrome 擴展中訪問當前選項卡的所有 window 變量
使用內容腳本將代碼插入頁面上下文

PS。 我只在 Firefox 上測試過,因為我不使用 Chrome。

更新 2

查看Can't find page variables when used GM_ functions時,似乎 GM|TM|VM 可能在沒有@grant none時將用戶腳本注入到頁面內容中(需要適當的確認)。 這可以解釋為什么上面帶有@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] 頁面 CSP 不應應用於內容腳本插入的內容(V2 問題)
CSP 'sandbox' 指令阻止內容腳本匹配,由於獨特的來源,也破壞了瀏覽器功能 [屏幕截圖]

有一些方法可以解決這個問題,但它們不是標准的,擴展不應繞過瀏覽器或頁面 CSP。

我很確定 user_script 而不是 content_script 是通往 go 的方式。

此 API 提供與 contentScripts 類似的功能,但具有適合處理第三方腳本的功能:

  • 訪問 window 並記錄與用戶腳本附加到的網頁相關的全局值。

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

雖然它的工作方式有點不同,但仍然試圖理解它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM