我一直在网上搜索以了解如何访问当前选项卡 DOM 以从 background.html 的页面中提取信息。到目前为止,我没有运气,或者至少没有什么可以正常工作。
简单地说一下问题。我想在页面上获取 iFrame 的 src。有什么建议么?
我一直在网上搜索以了解如何访问当前选项卡 DOM 以从 background.html 的页面中提取信息。到目前为止,我没有运气,或者至少没有什么可以正常工作。
简单地说一下问题。我想在页面上获取 iFrame 的 src。有什么建议么?
一种方法是,您可以将其视为对内容脚本的一次性请求,该请求将获取您要访问的 dom。 http://code.google.com/chrome/extensions/messaging.html#simple
基本上,您的内容脚本设置了侦听器:
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、文本等)。
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.