Javascript 扩展can';t访问窗口对象中的自定义函数
当Chrome扩展试图通过Chrome.executeScript在window对象中获取自定义函数时,它什么也得不到 例如: 标签编号:150 选项卡js:Javascript 扩展can';t访问窗口对象中的自定义函数,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,当Chrome扩展试图通过Chrome.executeScript在window对象中获取自定义函数时,它什么也得不到 例如: 标签编号:150 选项卡js: window.customfunc = function(){return 'yeap';} 扩展的背景JS: chrome.tabs.executeScript(150, { code: "console.log(window);" }) { "background": { "scripts": [ "backgr
window.customfunc = function(){return 'yeap';}
扩展的背景JS:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
{
"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"
}
Manifest.json:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
{
"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"
}
结果:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
{
"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"
}
在控制台中,窗口
对象不显示customfunc
,因此我们不能将window.customfunc
与chrome.executeScript
一起使用
为什么会发生这种情况,我们如何解决它?
谢谢。这是为了安全。内容脚本(正在执行的
background.js
)与选项卡的脚本(其中定义了customfunc
)隔离。您还可以获得不必担心名称空间冲突的额外好处(这正是您将事情搞砸的地方)
一种方法是创建myscript.js上面写着
console.log(窗口)
然后使用内容脚本(或
chrome.tabs.executeScript
)编写
到选项卡的DOM。您还需要添加
“web可访问资源”:[“myscript.js”]
到你的舱单 但是有了这么复杂的事情,一个好问题是为什么需要访问
customfunc
。(您还可以查看以获得更长的答案。)