【发布时间】:2025-12-29 23:45:16
【问题描述】:
我正试图将脚浸入正在创建镀铬扩展的池水中。我试着从我认为很简单的东西开始,但事实证明它让我很困惑,因为我真的不知道在哪里可以阅读关于这类事情的信息。
无论如何,目前我的目标是拥有一个执行以下操作的 chrome 扩展: 您单击扩展图标 -> 一个 HTML 弹出文件打开并显示单个按钮 -> 当您单击此按钮时,它会在新的 chrome 选项卡中打开一个指定的链接,并将其更改为您的活动选项卡。
从我的搜索中,我发现我可能必须使用后台脚本,这样我才不会违反关于 Manifest 版本 2 或类似内容的政策,我试过了,但它并没有真正适合我.如果这不是问题,我真的不想去创建一个全新的脚本。
这是我的 manifest.json 文件,后面是我的 popup.html 文件。
{
"manifest_version": 2,
"name": "strong text",
"author": "authorname",
"description": "desctext",
"version": "1.4",
"permissions": [
"tabs"
],
"icons": { "16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
"browser_action": {
"default_popup": "popup.html"
}
}
<!doctype html>
<html>
<head>
<title>Hovertext</title>
<script src="popup.js"></script>
</head>
<body>
<div id="header">
<h1>Header</h1>
</div>
<div id="divider">
<p><button type="button" id="buttonA">Button Text</button>
<script>
var button1 = document.getElementById("buttonA");
button1.addEventListener("click", function() {
chrome.tabs.create({url: "http://www.google.com/"});
});
</script>
</div>
<div id="footer">
footer stuff here
</div>
</body>
<style>
body {
background-image: url("https://s-media-cache-ak0.pinimg.com/736x/51/3e/4f/513e4f5274ec48f29b894b0b8409658f.jpg");
}
#header {
background-color:rgb(96, 222, 72);
text-align:center;
padding:1px;
}
#footer {
background-color:rgb(96, 222, 72);
clear:both;
text-align:center;
padding:1px;
}
#divider {
text-align:center;
}
#buttonA {
color:white;
background-color:rgb(0, 152, 255);
border-width:3px;
border-color:white;
}
#buttonA:hover {
color:rgb(0, 152, 255);
background-color:white;
border-width:3px
border-color:rgb(0, 152, 255);
}
</style>
</html>
我对这种类型的东西和堆栈溢出一般是新手,所以如果我没有正确澄清某些事情,请告诉我,提前感谢您的帮助!
【问题讨论】:
-
这个链接非常感谢@Rio,尽管我不太确定如何更改 sn-p
chrome.browserAction.onClicked.addListener(function(activeTab) { var newURL = "http://www.google.com/"; chrome.tabs.create({ url: newURL }); });以在 popup.html 中的按钮上使用。我开始认为我可能需要一个后台脚本来在后台监听按钮推送。
标签: javascript jquery html google-chrome