chrome拡張を使ってpopupのテキストボックス内に入力した値を
特定のwebページのテキストボックスに入力したいのですが
自作したpopupのテキストボックスから値の取得ができません。
やりたいことのイメージは以下の図です
popupのボタン押下後コンソール画面にて
Uncaught ReferenceError: textname is not defined
のエラーがでておりおそらく
textname = document.forms[0].elements[0].value;
ここで代入ができていないのだと思うのですが正しい代入方法をご教授いただきたいです。
お手数おかけしますがよろしくお願いします。
以下がhtmlとjsファイルとmanifest.jsonです
popup.html
1<!DOCTYPE html> 2<html> 3<body style="min-width:100px"> 4<!--<input type="button" id="id_Red" value="Red"> 5<br><br> 6<label for="uname">入力してください: </label> 7<input type="text" name="textname" size="15" maxlength="20" value="川口">--> 8<form id="form"> 9 <input type="text" name="inputnamet" size="15" maxlength="20" value></input> 10 <input type="submit" class="btn btn-primary" value="保存"> 11</form> 12 13<script src="myscript.js"></script> 14</body> 15</html>
myscript.js
1var textname ; 2 3 4document.getElementById('form').onsubmit =function () { 5 textname = document.forms[0].elements[0].value; 6 console.log(textname); 7 chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { 8 chrome.scripting.executeScript({ 9 target: {tabId: tabs[0].id}, 10 function:nameOutput 11 }); 12 }); 13} 14 15function nameOutput(){ 16 document.getElementById("w2firstName").value = textname; 17 console.log(textname); 18 19} 20
manifest.json
1{ 2 "name": "Chapter 5 (MV3)", 3 "version": "1.0", 4 "manifest_version": 3, 5 "description": "This is Chapter 5", 6 "permissions": ["scripting"], 7 "host_permissions": [ 8 "http://*/*", 9 "https://*/*" 10 ], 11 "action": { 12 "default_icon": "Chapter_5.png", 13 "default_title": "I am Chapter 5", 14 "default_popup": "popup.html" 15 } 16}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/14 16:06