[英]Chrome extension: Focus input field on main page
我有一個帶有輸入框和按鈕的HTML頁面。 我想要一個可以擴展到輸入元素的Chrome擴展程序。
HTML頁面-
<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>
我注入的腳本-
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");
}
});
我得到了啟動日志,但是我的元素沒有集中。
其余文件(即popup.html和popup.js)未顯示,因為它們工作正常且與此無關。
我嘗試模擬輸入元素上的鼠標單擊事件(從此處開始 ),但該方法也失敗了。
對此任何幫助表示贊賞。 向基於jQuery的解決方案開放。
謝謝。
HTML文檔准備就緒時發送消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.