Google Chrome扩展-访问DOM

Google Chrome扩展-访问DOM,dom,google-chrome,google-chrome-extension,Dom,Google Chrome,Google Chrome Extension,我一直在网上搜索,想知道如何访问当前的标签DOM,从background.html中提取页面信息。到目前为止,我没有运气,或者至少没有任何东西可以让我正常工作 简单地陈述问题。我想在页面上获得iFrame的src。有什么建议吗?一方面,您可以将此视为对内容脚本的一次性请求,内容脚本将获取您想要访问的dom。 基本上,内容脚本会设置侦听器: chrome.extension.onRequest.addListener( function(request, sender, sendRespon

我一直在网上搜索,想知道如何访问当前的标签DOM,从background.html中提取页面信息。到目前为止,我没有运气,或者至少没有任何东西可以让我正常工作


简单地陈述问题。我想在页面上获得iFrame的src。有什么建议吗?

一方面,您可以将此视为对内容脚本的一次性请求,内容脚本将获取您想要访问的dom。

基本上,内容脚本会设置侦听器:

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });
您的后台页面会发送一个实时请求:

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});
当您发送响应时,您将其作为JSON数据发送,您可以获取您想要的任何内容(如html、dom、文本等)


这是目前让背景页面了解页面内容的唯一方法。请记住,您需要内容脚本和选项卡权限。

一种方法是,您可以将此视为对内容脚本的一次性请求,内容脚本将获取您想要访问的dom。

基本上,内容脚本会设置侦听器:

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });
您的后台页面会发送一个实时请求:

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});
当您发送响应时,您将其作为JSON数据发送,您可以获取您想要的任何内容(如html、dom、文本等)


这是目前让背景页面了解页面内容的唯一方法。请记住,您需要内容脚本和选项卡权限。

请注意,此答案已过时。”SendRequest和getSelected不推荐使用。提供的脚本不起作用。aa在Chrome 20中应替换为sendMessage/onMessage。请注意,此答案已过时。”SendRequest和getSelected不推荐使用。提供的脚本不起作用。aa应在Chrome 20中替换为sendMessage/onMessage