chrome extension无法获取window对象

背景见上一篇博客修改网页内容的方法

上一篇博客之后,我要修改的网页有一个新改版,然后有个数据存在了window中,我直接在js中使用window.xxx发现无法获取。所以有本文。

https://juejin.cn/post/7145749643316428830

https://onelinerhub.com/chrome-extension/window_object#:\~:text=chrome-extension How to access or modify window object,%3D 'console.log (window)%3B'%3B (document.head||document.documentElement).appendChild (script)%3B ctrl %2B c

搜了一下发现有两种方法,如上,大概就是:

方法1

javascript 复制代码
var script = document.createElement('script');
script.textContent = 'console.log(window);';
(document.head||document.documentElement).appendChild(script);

出现报错:

bash 复制代码
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

json 复制代码
{
  "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"
    },
  ]
}
bash 复制代码
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.

报错比较清晰,就是注入的代码和原有代码不在一个域下,无法操作/注入javascript对象。

最后我是这样做的:

我的方法一

从其他页面获取这个数据,在url参数中携带这个信息;

我的方法二

从dom节点中获取第一个元素信息,然后通过网络请求,重新请求需要的数据。

相关推荐
此刻觐神18 小时前
IMX6ULL开发板学习-03(Linux文件相关命令)
前端·chrome
skywalk816321 小时前
使用DuMate帮助创建 Python 3.9 环境并部署 Kotti CMS
前端·chrome
卷卷说风控1 天前
工作流的 Skill 怎么写?
java·javascript·人工智能·chrome·安全
John_ToDebug2 天前
惰性绑定 vs 立即注入:Chromium 扩展 API 初始化策略深度对比
c++·chrome·v8
代码的乐趣2 天前
支持selenium的chrome driver更新到147.0.7727.56
chrome·python·selenium
海的透彻3 天前
nginx启动进程对文件的权限掌控
运维·chrome·nginx
chenjingming6663 天前
jmeter导入浏览器上按F12抓的数据包
前端·chrome·jmeter
John_ToDebug3 天前
Chrome 首次启动引导页里触发 Pref 设置,为什么主进程收不到 IPC
c++·chrome
小鹿软件办公3 天前
谷歌 Chrome 终于推出垂直标签页与更智能的阅读模式
前端·chrome
John_ToDebug3 天前
Chromium 页面类型与 IPC 通信机制深度解析
前端·c++·chrome