chrome扩展程序开发(在谷歌浏览器添加扩展插件时,勾选了开发模式,但...)

案例 2020-01-18 01:20:17

谷歌浏览器扩展开发,怎么样才能调用网页的JS函数

我摘抄了一段官方文档中关于这一点的解释:

Execution environment

Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.

摘自:网页链接

其中提到了 content scripot 的执行环境和页面原来的直用环境其实并不是同一个环境,而是一个隔离的环境,他们相互之间并不影响。也就说你访问不了原来页面里的变量,原来的页面也访问不了你新创建的变量。

但是,我们可以通过另外的方法与原页面通信,可以参考上文官方链接中 Communication with the embedding page 这节内容。

另外,还有一种办法,content script 执行在隔离环境,但 content script 可以通过 js 动态插入另一个

moxingzu

文章:57 问答:0