Chrome扩展程序:onclick()事件未触发alert()弹出窗口

3

我无法通过 onclick() 事件触发一个 alert() 弹窗。

代码

文件 manifest.json

{
    "name": "Project",
    "version": "1.0.0",
    "manifest_version": 2,
    "description": "Popup when website requires Log in",
    "browser_action":{
        "default_icon":"icon_19.png",
        "default_popup":"Popup.html"
        }
}

File Popup.html

<html>
<head></head>

<body>
    <div class="plus" onclick="popup()"></div>
    <script src="inline.js"></script>
</body>
</html>

文件 inline.js

function popup() {
    var link = document.URL;
    alert("This is the link: (" + link + ")");
}

请将所有的内联代码移动到外部文件中,包括 onclick 事件。不要在文本中使用任何行内代码。 - BeardFist
@BeardFist 非常感谢。我该如何获取网站的URL?它只显示弹出窗口的URL。 - anandh199g
你可以使用chrome.tabs.queryactive:true来获取当前标签页,然后使用tab.url来获取URL。 - BeardFist
1个回答

5
不要在Google Chrome扩展中使用内联JavaScript代码。
HTML:
<div class="plus"></div>
<script src="inline.js"></script>

JavaScript:

function popup(e) {
  var link = document.URL;
  alert("This is the link: (" + link + ")");
}

var plusBtn = document.querySelector('.plus');
plusBtn.addEventListener('click', popup);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接