在content.js(Chrome扩展程序)中发生操作时,如何检查popup.html页面?

时间:2018-08-01 19:28:23

标签: javascript html google-chrome-extension

我的chrome扩展程序需要检查扩展程序处于活动状态的页面上popup.html上是否已选中复选框。

content.js:

$(document).ready(function () {
  time = setInterval(function () {
    if(true){
      //check if checkbox is checked??
    }
  }, 500);
});

popup.html:

<!doctype html>
<html>
<head>
<input type="checkbox" name="thingToDo" value="isChecked"> Do thing<br>
</body>
</html>

是否有一种方法可以从content.js中抓取popup.html?还是我需要background.js和某种来回发送的消息?

1 个答案:

答案 0 :(得分:0)

您无法在popup.htmlcontent.js之间进行直接通信,但是通常的方法是使用chrome.storage API。

通过checked设置popup.html状态,并监听content.js中的存储更改。

因此,无论何时或从何处更改存储值,您都将进入content.js

这也是简便方法。