Google Chrome扩展程序 - 访问DOM [英] Google Chrome Extension - Accessing The DOM

查看:160
本文介绍了Google Chrome扩展程序 - 访问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?

推荐答案

一种方式,您将此视为对内容脚本的单次一次请求,将会获取你想要访问
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,text等)。

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.

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

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