每次使用弹出窗口时显示一个新链接

时间:2019-04-18 16:22:18

标签: google-chrome-extension

我的目标是构建一个Google chrome扩展程序,该扩展程序在每次使用时都显示指向不同网站的链接。

也就是说,我有一个网站列表,我想遍历该列表,以便每次单击扩展名时都显示下一个链接。

当前,我的popup.html文件看起来像这样

<!DOCTYPE html>
<html>
<head>
<title>My First Chrome Extension</title>
<style>
    #popup{
        width:300px;
        height:200px;
        text-align:center;
        line-height:200px;
    }
</style>
    <script src="popup.js"></script>
 </head>
<body>
<div id="popup">
        <a href="some_link.com" target="_blank">next link</a>
</div>
</body>
</html>

(popup.js文件为空)。当我点击扩展程序图标时,它将显示指向“ some_url.com”的链接,该链接会在新标签页中打开(大型)。我要寻找的是当我再次单击扩展图标时能够显示“另一个链接”(“另一个链接”是某些预定义列表中的下一个)。

我非常感谢帮助/资源能够将此行为添加到扩展中。我是chrome扩展程序(和javascript)的新手,不知道下一步该怎么做。

1 个答案:

答案 0 :(得分:0)

您可以在popup.html页面上创建带有某些ID的锚标记

<a id="show-links" target="_blank"></a>

然后在您的popup.js文件中,使用javascript将hfref属性添加到此元素

var a = document.getElementById('show-links');
//create an array with all links
links_array = ["some_link.com", "some_link1.com", "some_link2.com"];
//assign links to a from array at random
a.href = links_array[Math.floor(Math.random() * links_array.length)];