問題描述
我做了一個(gè) Firefox 擴(kuò)展來獲取所有請求的 url 并顯示它們.但代碼只有在我將其粘貼到控制臺時(shí)才有效.
I make a firefox extension that get all the request url's and displays them. But the code only works if I paste it in the console.
當(dāng)擴(kuò)展程序加載時(shí)它沒有顯示任何錯(cuò)誤,它似乎只是不會運(yùn)行
when the extension loads it doesn't show any error, it seems like it just won't run
這是完整的代碼
xhrScript.js
ma??nifest.json
如您所見,最后一行是 document.body.style.border = "7px solid blue";
,每次都可以正常工作.但是 XMLHttpRequest open
和 send
方法不起作用.僅當(dāng)我將代碼粘貼到控制臺時(shí)才有效.
As you can see, in the last line is document.body.style.border = "7px solid blue";
, this works fine every time. But the XMLHttpRequest open
and send
methods don't work. only works if I paste the code in the console.
如果您想查看示例,可以嘗試將 xhrScript.js
代碼復(fù)制并粘貼到 https://reactjs.org(這是一個(gè) SPA,所以很容易檢查我想要什么)在 devTools 控制臺中,并查看所有請求.
if you want see an example, you can try copy and paste the xhrScript.js
code in https://reactjs.org (it's a SPA, so it's easy to check what I want) in the devTools console, and see all the request.
我不知道為什么這段代碼只有在控制臺粘貼時(shí)才會運(yùn)行
I don't know why this code only runs when it is pasted in console
推薦答案
內(nèi)容腳本在隔離的 JavaScript 環(huán)境中運(yùn)行,這意味著 window
及其內(nèi)容與頁面隔離,因此當(dāng)您修改它時(shí),您只修改內(nèi)容腳本的版本.
Content scripts run in an isolated JavaScript environment meaning that window
and its contents are isolated from the page so when you modify it, you only modify the content script's version.
有兩種解決方案:
Firefox 專用.
Firefox-specific.
使用 wrappedJSObject
和 exportFunction
訪問頁面上下文(更多信息):
Use wrappedJSObject
and exportFunction
to access the page context (more info):
Chrome 兼容.
Chrome-compatible.
使用 DOM 腳本在頁面上下文中運(yùn)行代碼:說明.
它不適用于受嚴(yán)格的 Content-Security-Policy (CSP) 保護(hù)的頁面,該 CSP 會阻止腳本執(zhí)行,因此在為 Firefox 編寫擴(kuò)展時(shí),我們應(yīng)該改用 wrappedJSObject
方法.
It won't work on pages protected by a strict Content-Security-Policy (CSP) that prevents script execution so when writing an extension for Firefox we should use wrappedJSObject
method instead.
這篇關(guān)于為什么這段代碼不起作用?我正在創(chuàng)建一個(gè) Firefox 擴(kuò)展,但代碼沒有運(yùn)行.但是,如果我將代碼粘貼到控制臺中,它就可以工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!