Google Chrome Extension - Accessing The DOM

不想你离开。 提交于 2019-11-27 08:07:57

One way, you an treat this as a single one time request to the content-script which will fetch the dom you want to access. http://code.google.com/chrome/extensions/messaging.html#simple

Basically, your content script sets up the listener:

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });

And your background page sends a single lived request:

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

When you send your response, you send it as JSON data, you can fetch whatever you want (such as html, dom, text, etc).

That is currently the only way to let the background page know anything about the contents of a page. Remember you would need content scripts and tab permissions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!