Chrome网页扩展程序,使用按钮在新标签页中打开网页

问题描述 投票:0回答:2

我是第一次开发chrome扩展。它相对简单,我想要它做的是在单击按钮时在新选项卡中打开一个网页。我不知道如何在没有javascript的情况下这样做,因为我知道chrome块内联<script>元素(或类似的东西)。下面是我的popup.html

<!DOCTYPE html>
<html>
<head>


<style>

.button {
    position: relative;
    background-color: #4F5B62;
    border: none;
    font-size: 28px;
    font-family: "Roboto Mono";
    font-weight: lighter;
    color: #FFFFFF;
    opacity: 0.6;
    padding: 20px;
    width: 200px;
    text-align: center;
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
    text-decoration: none;
    overflow: hidden;
    cursor: pointer;
    border-radius: 12px;
}

.button:hover{
  opacity: 1;
}

.button:after {
    content: "";
    background: #f1f1f1;
    display: block;
    position: absolute;
    padding-top: 300%;
    padding-left: 350%;
    margin-left: -20px !important;
    margin-top: -120%;
    opacity: 0;
    transition: all 0.8s
}

.button:active:after {
    padding: 0;
    margin: 0;
    opacity: 1;
    transition: 0s
}

body{
  background-color: #263238;
}

head{
  background-color: #263238;
}
</style>
</head>
<body>


<button type="button" class="button">Access chatter</button>

</body>
</html>

任何帮助,将不胜感激。

javascript html json google-chrome google-chrome-extension
2个回答
1
投票

的manifest.json

首先,您需要添加在chrome.tabs中使用manifest.json API的权限。

{
   ...
   "permissions": ["tabs"],
   ...
}

popup.html

然后你可以在你的按钮和popup.js标签底部的<body>脚本中添加一个id。

<body>
   <button type="button" class="button" id="btn1">Access chatter</button>
   <script src="popup.js"></script>
</body>

popup.js

最后在脚本中添加按钮操作。

Using vanilla JavaScript

var button = document.getElementById("btn1");
button.addEventListener("click", function(){
    chrome.tabs.create({url:"http://www.google.com/"});
});

Using jQuery

如果使用jQuery,请务必在popup.js上面添加相应的脚本。

$('#btn1').click(function() {
   chrome.tabs.create({url:"http://www.google.com/"});
});

0
投票

在这里,您可以阅读有关chrome扩展qazxsw poi入门的信息

在这里,您可以看到创建新选项卡https://developer.chrome.com/extensions/getstarted的参考

© www.soinside.com 2019 - 2024. All rights reserved.