mik*_*lan 10 dom google-chrome google-chrome-extension
我一直在网上搜索,以了解如何访问当前标签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.
});
Run Code Online (Sandbox Code Playgroud)
并且您的后台页面发送一个有效的请求:
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
Run Code Online (Sandbox Code Playgroud)
当您发送响应时,将其作为JSON数据发送,您可以获取所需的任何内容(例如html,dom,text等).
这是目前让背景页面了解页面内容的唯一方法.请记住,您需要内容脚本和标签权限.
| 归档时间: |
|
| 查看次数: |
13010 次 |
| 最近记录: |