简体   繁体   English

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

[英]How to check popup.html page when action occurs within content.js (Chrome Extension)?

My chrome extension needs to check if a checkbox is checked on the popup.html while on the page that the extension is active. 我的Chrome扩展程序需要检查扩展程序处于活动状态的页面上popup.html上是否已选中复选框。

content.js: content.js:

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

popup.html: popup.html:

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

Is there a way to kind of scrape the popup.html from content.js? 有没有一种方法可以从content.js中抓取popup.html? Or will I need a background.js and some sort of message sending back and forth? 还是我需要background.js和某种来回发送的消息?

You can't do a direct communication between popup.html and content.js , but the usual way is to use chrome.storage API. 您无法在popup.htmlcontent.js之间进行直接通信,但通常的方法是使用chrome.storage API。

Set the checked status via popup.html and listen for storage changes in content.js . 通过popup.html设置checked状态,并监听content.js存储更改。

So whenever or from whereever the storage value gets changed, you will get in the content.js 因此,无论何时何地更改存储值,您都可以进入content.js

It's the easy way and clean way too. 这也是简单方法和干净方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM