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节点中获取第一个元素信息,然后通过网络请求,重新请求需要的数据。

相关推荐
John_ToDebug2 小时前
浏览器内核崩溃深度分析:从 MiniDump 堆栈到 BindOnce UAF 机制(未完待续...)
c++·chrome·windows
John_ToDebug4 小时前
WebContent 与 WebView:深入解析浏览器渲染架构的双层设计
c++·chrome·ui
老兵发新帖12 小时前
Ubuntu版本nvidia-smi提示版本不匹配问题,解决办法
linux·chrome·ubuntu
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.1 天前
Nginx构建PC站点:root与alias详解
运维·chrome·nginx
goxingman2 天前
在 Linux 中查看磁盘运行占用(I/O 使用率)
linux·运维·chrome
铁蛋AI编程实战3 天前
Gemini in Chrome 全实战:解锁+API调用+自定义扩展+本地推理
前端·人工智能·chrome
hbstream海之滨视频网络技术3 天前
Google正式上线Gemini In Chrome,国内环境怎样开启。
前端·chrome
铁蛋AI编程实战3 天前
谷歌MedGemma 1.5医疗大模型开源部署教程:普通显卡可运行,医学影像分析零代码实现
人工智能·chrome·开源
vortex53 天前
深度字典攻击(实操笔记·红笔思考)
前端·chrome·笔记
life码农4 天前
Linux系统清空文件内容的几种方法
linux·运维·chrome