Chrome extension: Focus input field on main page
298 观看
1回复
644 作者的声誉
I have one HTML page with input box and button. I want to have a chrome extension that can focus on the input element.
HTML page -
<html>
<body>
<input type="text" name="box" />
<button type="button" onclick="clicked()">Click</button>
<script>
//document.getElementsByName("box")[0].focus(); <--- this works. But I don't want this.
</script>
</body>
</html>
My injected script -
chrome.runtime.onMessage.addListener(function(message, sender, response){
if(message.action==="start"){
document.getElementsByName("box")[0].focus(); //<---want this to work. Currently unable to :(
console.log("started");
}
});
I get the started log but my element is not focused.
Rest files (i.e. popup.html and popup.js) not shown because they are working fine and irrelevant to this.
I tried simulating mouse click event on the input element (from here) but that method also failed.
Any help regarding this is appreciated. Open to jquery based solutions.
Thanks.
作者: aquaman 的来源 发布者: 2017 年 12 月 27 日回应 1
0像
481 作者的声誉
来自类别的问题 :
- javascript 如何检测网页中使用了哪一个定义的字体?
- javascript JavaScript对象的长度
- javascript 从下拉框中获取文本
- javascript 带隐藏按钮的登录脚本
- javascript 如何使用Prototype自动调整textarea?
- html 如何为我的网站提供iPhone的图标?
- html 如何在Web表单字段/输入标记上禁用浏览器自动完成?
- html 如何定制有序列表中的数字?
- google-chrome 如何在Google Chrome中启动JavaScript调试程序?
- google-chrome 使用JavaScript获取图像的实际宽度和高度?(在Safari / Chrome中)
- google-chrome 为什么Chrome没有其他浏览器时会报告安全/非安全警告?
- google-chrome Chrome中选择选项元素上的Click事件
- google-chrome 是否可以在Firefox或Chrome中查看帖子请求的数据?
- google-chrome-extension Chrome扩展程序:如何在磁盘上保存文件
- google-chrome-extension Javascript:让用户选择像Firebug这样的HTML元素?
- google-chrome-extension 是否有JavaScript / jQuery DOM更改侦听器?
- google-chrome-extension 如何自动重新加载我正在开发的Chrome扩展程序?
- google-chrome-extension 有没有办法在谷歌浏览器中获取xpath?