此扩展的目标是我可以在扩展中输入一些内容,然后按提交并发出警报以显示我的输入。
在我的 popup.html 中:
<form id="form" onsubmit="return false;">
<input type="text" id="userInput" />
<input type="submit" onclick="othername();" />
</form>
manifest.json:
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
},
和 content.js:
alert("test");
function othername() {
var input = document.getElementById("userInput").value;
alert(input);
};
出现“测试”警报,所以我知道他的代码正在运行,但是当我输入一个值并按提交时,我没有收到弹出窗口。任何帮助将不胜感激。
最佳答案
您无法从 HTML 中调用函数。 尝试在 JavaScript 中添加一个 click 事件监听器。
document.getElementById('clickbutton').addEventListener('click', function() {
alert('hello ' + document.getElementById("userInput").value);
});
我刚刚为此目的创建了一个新的 Chrome 扩展程序。
看看:
https://github.com/aldi/alert-input-chrome-extension
关于javascript - 在警报中返回用户输入的简单 Chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964681/