我正在制作一个 Chrome 扩展,我正在使用以下内容:
list .json
{
"name": "Test extension",
"version": "1.1",
"description": "Test extension.",
"icons": {
"128": "icon_128.png"
},
"chrome_url_overrides": {
"newtab": "cc.html"
},
"manifest_version": 2
}
cc.html
<style>body,html{padding:0;margin:0}</style>
<iframe src="theiframe.html" frameborder="0" height="200px" width="200px">
</iframe>
theiframe.html
<style>body,html{padding:0;margin:0}</style>
<form action="http://www.example.com/search">
<input autofocus="autofocus" tabindex="1" type="text" />
<input tabindex="2" value="search" type="submit"/>
</form>
当用户打开新标签时,自动对焦 将出现在地址栏中。我希望用户能够改变它。是否有任何代码可以自动自动聚焦搜索输入?
最佳答案
也许仅使用 iframe
是行不通的。但是您可以制作一个示例页面,当打开新选项卡时,该页面将重定向到您想要的页面,因此具有 autofocus
的 input
将自动聚焦。
这里有一个方法:
list .json
"chrome_url_overrides": {
"newtab": "r.html"
},
r.html
<html>
<head>
<title>Loading...</title> <!-- user friendly -->
<noscript>
<meta http-equiv="refresh" content="0; url=https://www.google.com"> <!-- in case javascript is disabled -->
</noscript>
<script src="s.js"></script>
</head>
<body></body>
</html>
s.js
window.location="https://www.google.com";
这是最好的方法(也可能是唯一的方法)。
关于javascript - Chrome 新标签自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14653857/