所以我前几天制作了一个小 html 文件,它只是一个带有时钟、gif 背景和一个用于搜索 Google 的搜索栏的页面。
我之前把它作为一个 html 文件工作,但后来我想把它变成一个 chrome 扩展。我必须制作一个 manifest.js 文件并将脚本也放入一个单独的文件中,然后搜索就不再起作用了。
我真的不知道我做错了什么。
这是我的 list 文件:
{
"name": "THE GOODEST",
"description": "Overrides the new tab page",
"version": "0.1",
"icons": {"48": "icon.png"},
"incognito": "split",
"chrome_url_overrides": {
"newtab": "newTab.html"
},
"manifest_version": 2
}
这是我的 html 文件:
<!DOCTYPE html>
<html>
<title>New Tab</title>
<body onload = "resizeWindow()">
<iframe src="http://free.timeanddate.com/clock/i63s4yyj/n224/ahl/avb/pa10/tt0/tw1/tm1/ts1" frameborder=0 width="195" height="37" style="position:fixed; bottom: 10px"></iframe>
<input id="searchBar">
<button onclick="search()">Search</button>
<script type = "text/javascript" src = "scripts.js"></script>
</body>
</html>
这是我的脚本:
function search() {
var text = document.getElementById("searchBar").value;
window.open("https://www.google.com/search?q=" + text)
}
最佳答案
正如 @Deliaz 在评论中所述,由于严格的环境隔离,Chrome 扩展中不允许使用内联 JavaScript。
更改此:<button onclick="search()">Search</button>
至:<button id='searchButton'>Search</button>
并在单独的 script.js 文件中放置一些代码,将事件监听器附加到点击操作上的搜索按钮。
// Triggers when all DOM elements are loaded
document.addEventListener('DOMContentLoaded', function(event) {
// Attaches listener for click event on search button
document.querySelector('#searchButton').addEventListener('click', function(e) {
return search();
});
});
function search() {
var text = document.getElementById("searchBar").value;
window.open("https://www.google.com/search?q=" + text)
}
或者,您也可以在输入字段上使用搜索事件:https://developer.mozilla.org/en-US/docs/Web/Events/search
关于javascript - Chrome 扩展程序中的功能无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48716896/