everyone. Can anybody tell me how I can access DOM of another pages in my extension from content script or background page? I tried to make a connection from content script to popup.html. But it doesn't work. But the same code works with background.html... Here's the code I place in myscript.js:
function sendLast(){
  var result = document.getElementById('last_3212164');  //document.evaluate("//*[@id='last_3212164']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
  alert(result.textContent);
  chrome.extension.sendRequest({greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
}
var t=setTimeout("sendLast()", 3000);
Here is code from popup.html:
<html>
  <head>
        <script>
chrome.extension.onRequest.addListener(
  function(request, sender, sendRes开发者_如何转开发ponse) {
    alert("Got it!");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  }
);
    </script>
  </head>
<body>
Here will be the values
</body>
</html>
This code is supposed to give alert "Got it" every 3 second. But It doesn't do anything, silence... I open Popup.html 1 sec after loading the main page.
If popup is currently opened then you can access its DOM from a background page. If you need it in a content script then you would need to send a request to a background page first.
Lets say you want to read a value of test input field from a popup inside a content script:
content_script.js:
chrome.extension.sendRequest({cmd: "findElementInPopup"}, function(response){
    console.log("value:", response);
});
background.html:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "findElementInPopup") {
        var winArray = chrome.extension.getViews({type:"popup"});
        //if popup is opened
        if(winArray.length > 0) {
            sendResponse(winArray[0].document.getElementById("test").value);
        }
    }
});
You would need to do all the DOM manipulations in a background page as trying sending whole window through a response back to a content script would result in an error (object structure is too complex for json serialization).
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论