我一直在网上搜索以了解如何访问当前标签 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、文本等)。
这是目前让后台页面了解页面内容的唯一方法。请记住,您需要内容脚本和选项卡权限。
关于dom - Google Chrome 扩展 - 访问 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2063015/