如何添加使用 Chrome 扩展程序标记当前打开的页面?
我想当用户单击 chrome 扩展图标时将 div 标签添加到当前打开的网站。该图像应出现在页面的左上角。我怎样才能实现这个目标?
下面的代码将 div 标签添加到扩展窗口。
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function() {
$("#click").click(function(){
chrome.tabs.getSelected(null, function(tab){
var tabUrl = tab.url;
//alert(tabUrl);
document.querySelector('div#content').style.display = 'block';
});
//chrome.tabs.executeScript(null, {code: "document.body." + setAttribute("class",img)});
});
});
</script>
</head>
<body>
<div id="content"></div>
<div id="click">Click Here</div>
</body>
</html>
I want to add div tag to the currently open website when user clicks chrome extension Icon. This image should appear on the left top corner of the page. How can I achieve this goal?
The code below adds div tag to extension window.
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function() {
$("#click").click(function(){
chrome.tabs.getSelected(null, function(tab){
var tabUrl = tab.url;
//alert(tabUrl);
document.querySelector('div#content').style.display = 'block';
});
//chrome.tabs.executeScript(null, {code: "document.body." + setAttribute("class",img)});
});
});
</script>
</head>
<body>
<div id="content"></div>
<div id="click">Click Here</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据推测,图标指的是 browserAction,即多功能框右侧的按钮。绑定到 chrome.browserAction.onClicked 事件,并运行executeScript在当前选项卡上,注入一个添加
position:absolute; 的脚本顶部:0; left: 0;
图像到页面的 DOM。这是一个很好的示例,可以帮助您入门(点击时更改页面颜色浏览器操作)
Presumably by icon you mean browserAction, the button on the right of the omnibox. Bind to the chrome.browserAction.onClicked event, and run executeScript on the current tab, injecting a script that adds a
position: absolute; top: 0; left: 0;
image to the DOM of the page.Here's a good sample to get you started (changes the page color on clicking the browser action)
将元素附加到 DOM 中是通过内容脚本完成的,这些脚本在页面加载事件之后运行。正如您在 chrome 扩展文档中看到的那样,因此它们不会影响已打开的选项卡。
appending elements into DOM is fulfilled by content scripts, those scripts run after page load event. as you can see in the chrome extension documentations, so they won't affect already opened tabs.