2016-11-01 11 views
0

私はポップアップのデータをフォームに塗りつぶす拡張子を作成しようとしていますが、「背景」と「コンテンツ」ファイルの使用に関してはちょっと混乱しています。私が必要とは思わない。ここに私のコードは次のとおりです。popup.htmlのデータをフォームに入力してください

マニフェスト:

{ 
    "name": "Browser action for Form", 
    "description": "Fill a form with data from the popup", 
    "version": "1.0", 
    "permissions": [ 
    "tabs", "http://*/*", "https://*/*" 
    ], 
    "browser_action": { 
     "default_title": "Form Test", 
     "default_icon": "icon.png", 
     "default_popup": "popup.html" 
    }, 
    "content_scripts": [ 
    { 
     "matches": ["https://the-site-with-a-form.com/*"], 
     "js": ["jquery-3.1.1.min.js", "content.js"] 
    } 
    ], 
    "manifest_version": 2 
} 

popup.html

<!doctype html> 
<html> 
    <head> 
    <title>Form</title> 
    <script src="popup.js"></script> 
    </head> 
    <body> 
    <form> 
     <textarea id="txtArea"></textarea> 
     <input type="button" id="btn1" value="Run"> 
    </form> 
    </body> 
</html> 

popup.js

function click(){ 
var text = document.getElementById("txtArea") 
chrome.tabs.sendMessage(
     tabs[0].id, 
    {from: 'popup', subject: 'DOMInfo',data1: text}); 

} 

content.js

chrome.runtime.onMessage.addListener(function (msg, sender, response) { 
if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) { 
//Fill the form with the data of the popup 
document.getElementById("The textbox from the form").value = msg.data1; 
} 
}); 

コードで何が間違っていますか? ありがとう!

答えて

0

debug extension popupsをご覧ください。そうした場合、有益なエラーメッセージが表示されます。

ポップアップコードのtabsはどこから来ていないので、コードはエラーで終了します。明らかに、その部分はコンテキスト(おそらくtabs.queryコール)から取り除かれています。現在アクティブなタブにメッセージを表示することを目的としている場合は、最初の引数を完全にsendMessageにスキップできます。

あなたはdefintiely を実行します。には、ウェブページのフォームとやり取りできる拡張機能の唯一の部分であるため、コンテンツスクリプトが必要です。推奨読書:ここHow do all types of Chrome extension scripts work?

+0

感謝を固定 "タブ" とpopup.jsです!確かに問題は "sendMessage"の "tabs"引数にあり、それを修正しました: var activeTab = tabs [0]; chrome.tabs.sendMessage(activeTab.id、{"message": "s" ........ が今すぐ動作します!ありがとうございました! – 123345566i

+0

あなた自身の解決策がある場合は、別の回答として投稿してください。 – Xan

0

は、引数

function click(e) { 

    chrome.tabs.query({currentWindow: true, active: true}, function (tabs){ 
     var activeTab = tabs[0]; 
     var text = document.getElementById("txtArea").value; 
     chrome.tabs.sendMessage(activeTab.id, {from: 'popup', subject: 'DOMInfo',data1: text}); 
     }); 
    window.close(); 
} 

document.addEventListener('DOMContentLoaded', function() { 
    document.getElementById('submitBtn').addEventListener('click', click); 
}); 
関連する問題