Javascript 谷歌浏览器扩展操作打开或当前选项卡的 DOM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10066100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Chrome Extension Manipulate DOM of Open or Current tab
提问by chris
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.
好的,JavaScript/jQuery 我明白了,我可以用它来做我想做的事情。但是,我目前正在尝试使用打开/当前选项卡的 DOM,我想知道这是否可行,或者我使用仅限于 html 的扩展所做的所有工作我为它提供了“背景。 html”或等效文件。
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.
对于今天试图在 google 扩展程序上进行罢工的尝试,我想在页面上拍摄图像并将它们存储在一个数组中,以通过一个栏创建类似幻灯片的效果,我想将其添加到页面底部,并将其附加到打开/当前选项卡的现有 DOM。然后我想在 DOM 中“隐藏”元素,直到栏关闭。
So my first question is, is something like this possible, can I manipulate the DOM in such a way and read from it.
所以我的第一个问题是,这样的事情是否可能,我可以以这种方式操作 DOM 并从中读取。
回答by Rob W
Content Scriptsare 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.
内容脚本是在页面和 Chrome 扩展程序之间的环境中运行的脚本。这些脚本在每次页面加载时加载,并且可以完全访问页面的 DOM。DOM 方法,例如它们的document.getElementById()
行为就好像它们是页面的一部分。
The global window
objects (including frames
and HTMLIFrameElement.contentWindow
) are the only objects which are not directlyreadable by Content scripts.
全局window
对象(包括frames
和HTMLIFrameElement.contentWindow
)是唯一不能被内容脚本直接读取的对象。
Content scripts run on every page as defined in the manifest file. Specify a match patternat 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.
内容脚本在清单文件中定义的每个页面上运行。在该部分指定匹配模式"content_scripts"
,以定义内容脚本必须在哪些页面上运行。还要将此模式添加到该"permissions"
部分,以解锁修改页面 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).
注意:只有少数chrome.*
API可以被这些脚本使用(例如chrome.extension.sendRequest
与后台页面通信)。
在一个 background page后台页面,页面的 DOM 不能直接访问。您可以使用在任意选项卡中注入脚本
chrome.extension.executeScript
chrome.extension.executeScript
,但你将无法获得 direct直接引用元素。为此,需要内容脚本。