当 Chrome 扩展程序尝试通过 chrome.executeScript 在窗口对象中获取自定义函数时,它什么也得不到。
例如:
标签 ID:150
标签js:
window.customfunc = function(){return 'yeap';}
扩展的后台JS:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
清单.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
,所以我们不能使用window.customfunc
with chrome.executeScript
。
为什么会发生这种情况,我们该如何解决?谢谢。