Google chrome extension 从内容脚本或背景页访问扩展内的其他页面

Google chrome extension 从内容脚本或背景页访问扩展内的其他页面,google-chrome-extension,Google Chrome Extension,各位。 有人能告诉我如何从内容脚本或背景页面访问扩展中其他页面的DOM吗?我尝试从内容脚本连接到popup.html。但它不起作用。但是同样的代码也适用于background.html。。。 以下是我在myscript.js中放置的代码: function sendLast(){ var result = document.getElementById('last_3212164'); //document.evaluate("//*[@id='last_3212164']", docum

各位。 有人能告诉我如何从内容脚本或背景页面访问扩展中其他页面的DOM吗?我尝试从内容脚本连接到popup.html。但它不起作用。但是同样的代码也适用于background.html。。。 以下是我在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);
以下是popup.html中的代码:

<html>
  <head>
        <script>
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    alert("Got it!");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  }
);

    </script>
  </head>
<body>
Here will be the values
</body>
</html>

chrome.extension.onRequest.addListener(
功能(请求、发送方、发送响应){
警惕(“明白了!”);
if(request.greeting==“hello”)
sendResponse({再见:“再见”});
其他的
sendResponse({});//怠慢它们。
}
);
下面是这些值

这个代码应该每3秒钟发出一次“收到”警报。但它什么也没做,安静。。。我在加载主页1秒钟后打开Popup.html。

如果当前打开了Popup,则可以从后台页面访问其DOM。如果在内容脚本中需要它,则需要首先向后台页面发送请求

假设您想从内容脚本内的弹出窗口中读取
test
输入字段的值:

content\u script.js:

chrome.extension.sendRequest({cmd: "findElementInPopup"}, function(response){
    console.log("value:", response);
});
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);
        }
    }
});
background.html:

chrome.extension.sendRequest({cmd: "findElementInPopup"}, function(response){
    console.log("value:", response);
});
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);
        }
    }
});

您需要在后台页面中执行所有DOM操作,因为尝试通过响应将整个
窗口发送回内容脚本会导致错误(对象结构对于json序列化来说太复杂)。

您能显示不起作用的代码吗?我已将其添加到问题中。这是谷歌代码网站的代码和平。主要问题是如何访问popup的DOM。htmlMy内容脚本嵌入到页面中。此页面使用AJAX更新,例如,我尝试每1秒从该页面获取一次值。所以,在我的例子中,我需要先将数据发送到后台页面,然后从后台页面写入popup的DOM?真的,我不在乎它是弹出的还是其他的页面。我只需要积累数据,并获得与此数据的用户csv文件。你能告诉我最好的方法是什么吗?另外,弹出窗口从一开始就关闭了。。。所以我从chrome.extension.getViews({type:“popup”})中什么也得不到@damluar如果您需要在弹出窗口中写入一些内容,那么为什么不直接向弹出窗口发送请求并在那里执行所有操作呢?弹出窗口必须是打开的,关闭的弹出窗口不能以任何方式访问(除非您自己通过ajax打开Popup.html)。谢谢您的帮助。是否有方法在后台页面中累积值,然后形成csv并在新选项卡中打开它?例如@damluar您的代码看起来不错,只需将所有警报替换为
控制台。日志
,警报在弹出窗口中不起作用,并阻止js执行。