chrome扩展之content scripts与web pages 交互

本文介绍了Chrome扩展的content scripts无法直接访问网页脚本的问题,以及如何利用window.postMessage进行通信,实现content scripts调用网页脚本的功能。通过创建script元素执行所需脚本,结合postMessage传递返回结果,成功解决了这一限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

严重吐槽一下优快云的blog,卧槽,我好不容易写完一篇,点完发表之后就一直loading,然后就让我登陆,然后就特么什么都没有了!!!!!

好了,重新写一遍吧。


发现问题

chrome扩展的content scripts是不能直接访问web pages中的scripts的变量和函数的。一个小需求,让我发现了这个问题,官网描述如下:

Here are some examples of what content scripts can do:
Find unlinked URLs in web pages and convert them into hyperlinks
Increase the font size to make text more legible
Find and process microformat data in the DOM

However, content scripts have some limitatio这里写代码片ns. They cannot:
Use chrome.* APIs, with the exception of:
extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
i18n
runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
storage
Use variables or functions defined by their extension’s pages

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值