背景見上一篇博客修改網(wǎng)頁內(nèi)容的方法
上一篇博客之后,我要修改的網(wǎng)頁有一個(gè)新改版,然后有個(gè)數(shù)據(jù)存在了window中,我直接在js中使用window.xxx發(fā)現(xiàn)無法獲取。所以有本文。
https://juejin.cn/post/7145749643316428830
https://onelinerhub.com/chrome-extension/window_object#:~:text=chrome-extension%20How%20to%20access%20or%20modify%20window%20object,%3D%20%27console.log%20%28window%29%3B%27%3B%20%28document.head%7C%7Cdocument.documentElement%29.appendChild%20%28script%29%3B%20ctrl%20%2B%20c
搜了一下發(fā)現(xiàn)有兩種方法,如上,大概就是:
方法1
var script = document.createElement('script');
script.textContent = 'console.log(window);';
(document.head||document.documentElement).appendChild(script);
出現(xiàn)報(bào)錯(cuò):
extensions_version_page.js:6 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*". Either the 'unsafe-inline' keyword, a hash ('sha256-XnQUOdrKm+cYqdkT/ljnkhjaYaitt2Xh3uRQxH/M9z0='), or a nonce ('nonce-...') is required to enable inline execution.
方法2
{
"name": "yeshen_extresion",
"version": "1.0",
"description": "yeshen test",
"manifest_version": 3,
"web_accessible_resource": [
{
"resources": [
"assets/*",
"js/contentScript.js"
]
}
],
"content_scripts": [
{
"matches": [
"https://innovation.pm.netease.com/v6/issues/*",
"https://web.pm.netease.com/v6/issues/*",
"https://uu.pm.netease.com/v6/issues/*",
"https://msdk.pm.netease.com/v6/issues/*"
],
"js": [
"extensions_issue_page.js"
],
"run_at": "document_start"
},
]
}
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*". Either the 'unsafe-inline' keyword, a hash ('sha256-XnQUOdrKm+cYqdkT/ljnkhjaYaitt2Xh3uRQxH/M9z0='), or a nonce ('nonce-...') is required to enable inline execution.
報(bào)錯(cuò)比較清晰,就是注入的代碼和原有代碼不在一個(gè)域下,無法操作/注入javascript對象。
最后我是這樣做的:
我的方法一
從其他頁面獲取這個(gè)數(shù)據(jù),在url參數(shù)中攜帶這個(gè)信息;文章來源:http://www.zghlxwxcb.cn/news/detail-698353.html
我的方法二
從dom節(jié)點(diǎn)中獲取第一個(gè)元素信息,然后通過網(wǎng)絡(luò)請求,重新請求需要的數(shù)據(jù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-698353.html
到了這里,關(guān)于chrome extension無法獲取window對象的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!