When a Chrome extension tries to get custom functions in window object via chrome.executeScript, it gets nothing.
For example:
Tab ID: 150
Tab js:
window.customfunc = function(){return 'yeap';}
Extension's background 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"
}
Result:
In console, the window
object doesn't show customfunc
, so we can't use window.customfunc
with chrome.executeScript
.
Why does this happen, and how can we fix it? Thanks.