谷歌浏览器扩展 - 访问 DOM [英] Google Chrome Extension - Accessing The DOM

查看:47
本文介绍了谷歌浏览器扩展 - 访问 DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索以找出如何访问当前标签 DOM 以从 background.html 中提取页面信息.到目前为止,我没有运气,或者至少我无法正常工作.

I have been searching around the web to find out how to access the current tabs DOM to extract information out of a page from the background.html. So far I have had no luck, or at least nothing I could get to work properly.

简单地说明问题.我想在页面上获取 iFrame 的 src.有什么建议吗?

To state the problem simply. I would like to get src of a iFrame on the page. Any Suggestions?

推荐答案

一种方式,您可以将其视为对内容脚本的单个一次性请求,该内容脚本将获取您想要访问的 dom.http://code.google.com/chrome/extensions/messaging.html#simple

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);
  });
});

当您发送响应时,您将其作为 JSON 数据发送,您可以获取任何您想要的内容(例如 html、dom、文本等).

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.

这篇关于谷歌浏览器扩展 - 访问 DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆