當Chrome擴展嘗試通過chrome.executeScript獲取窗口對象中的自定義函數時,它什麼都沒有。擴展無法訪問窗口對象中的自定義函數
例如:
標籤ID:150個
標籤JS:
window.customfunc = function(){return 'yeap';}
擴展的背景JS:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
的manifest.json:
{
"background": {
"scripts": [ "background.js" ]
},
"content_scripts": [ {
"exclude_globs": [ ],
"exclude_matches": [ ],
"include_globs": [ "*://*/*" ],
"js": [ "script.js" ],
"matches": [ "http://*/*" ],
"run_at": "document_idle"
} ],
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
"description": "Test",
"manifest_version": 2,
"name": "Workspace",
"permissions": [ "unlimitedStorage", "notifications", "clipboardWrite", "notifications", "clipboardRead", "management", "tabs", "history", "cookies", "idle", "storage", "webRequest", "webRequestBlocking", "contentSettings", "*://*/*" ],
"version": "1.0"
}
結果:
在控制檯中,window
對象不顯示customfunc
,所以我們不能chrome.executeScript
使用window.customfunc
。
爲什麼會發生這種情況,我們該如何解決? 謝謝。
我使用方法2b,它的工作原理!謝謝。 –