谷歌浏览器扩展程序操纵打开或当前选项卡的DOM [英] Google Chrome Extension Manipulate DOM of Open or Current tab

查看:174
本文介绍了谷歌浏览器扩展程序操纵打开或当前选项卡的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,JavaScript / jQuery我明白了,我可以用它来做我想做的事情。然而,我现在想要做的是与开放/当前选项卡的DOM一起工作,并且我想知道这是否可能,或者是我所做的扩展仅限于HTML的工作,我为它提供了一个背景。 HTML或同等学历。



今天尝试在google扩展中进行攻击时,我想在页面上拍摄图像并将其存储在数组中以创建幻灯片放映通过一个我想添加到页面底部的条添加到打开/当前选项卡的现有DOM中,从而实现它的效果。然后我想在DOM中隐藏元素,直到酒吧被关闭。所以我的第一个问题是,是否可能这样,我可以在这样的DOM中操作DOM吗?一种方式并从中读取。

解决方案

内容脚本是在页面和Chrome扩展之间的环境中运行的脚本。这些脚本会在每次加载页面时加载,并且完全访问页面的DOM 。 DOM方法(如 document.getElementById())表现得好像它们是页面的一部分一样。



全局窗口对象(包括 frames HTMLIFrameElement .contentWindow )是内容脚本不能直接读取的唯一对象。



内容脚本在每个页面上运行,如在清单文件中定义。在content_scripts匹配模式 $ c>部分,以定义内容脚本必须在哪些页面上运行。还要将这种模式添加到permissions部分,以解除修改页面DOM的能力。



<注意:只有一些 chrome。* APIs 可以被这些脚本使用(例如 chrome.extension.sendRequest 后台页面)。



后台页面中,DOM一个页面不能直接访问。您可以使用 chrome.extension在任意选项卡中注入脚本。 executeScript ,但是您将无法获取对元素的直接引用引用。为此,需要内容脚本。


Ok, JavaScript/jQuery I got that, I can work with that to do what I want in general. However what I am trying to do currently is work with the DOM of the open/current tab, and I am wondering if that is possible or is all the work I do with an extension limited to the html I provide it with a "background.html" or equivalent.

For todays attempt at taking a strike at a google extension I want to take images on a page and store them in an array to create a slide show like effect from it via a bar I want to add to the bottom of the page appending it to the existing DOM of the open/current tab. I then want "hide" elements in the DOM til the bar is closed.

So my first question is, is something like this possible, can I manipulate the DOM in such a way and read from it.

解决方案

Content Scripts are scripts which run in an environment between a page and the Chrome extension. These scripts are loaded on every page load, and have full access to the page's DOM. DOM methods, such as document.getElementById() behave as if they were a part of the page.

The global window objects (including frames and HTMLIFrameElement.contentWindow) are the only objects which are not directly readable by Content scripts.

Content scripts run on every page as defined in the manifest file. Specify a match pattern at the "content_scripts" section, to define on which pages the content script has to run. Also add this pattern to the "permissions" section, to unlock the ability to modify the page's DOM.

Note: Only a few of the chrome.* APIs, can be used by these scripts (such as chrome.extension.sendRequest to communicate with the background page).


In a background page, the DOM of a page is not directly accessible. You can inject scripts in an arbitrary tab using chrome.extension.executeScript, but you won't be able to get a direct reference to an element. For this purpose, Content scripts are required.

这篇关于谷歌浏览器扩展程序操纵打开或当前选项卡的DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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