ポップアップからextension.jsへのデータ送信が機能しない
-
21-12-2019 - |
質問
私はクロスライダーを使用したブラウザ拡張機能に取り組んでいます。ポップアップからextension.jsにデータを送信する必要があります
私のポップアップのコード
<!DOCTYPE html>
<html>
<head>
<!-- This meta tag is relevant only for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript">
/************************************************************************************
This is your Popup Code. The crossriderMain() code block will be run
every time the popup is opened.
For more information, see:
http://docs.crossrider.com/#!/api/appAPI.browserAction-method-setPopup
*************************************************************************************/
function crossriderMain($) {
// var to store active tab's URL
var activeTabUrl = null;
// Message listener for response from active tab
appAPI.message.addListener(function(msg) {
if (msg.type === 'active-tab-url') activeTabUrl = msg.url;
});
// Request URL from active tab
appAPI.message.toActiveTab({type: 'active-tab-url'});
alert(activeTabUrl);
// THE REST OF YOUR CODE
}
</script>
</head>
<body>
Hello World
</body>
</html>
Extension.jsのコード
appAPI.ready(function($) {
// Message listener
appAPI.message.addListener(function(msg) {
if (msg.type === 'active-tab-url')
// Send active tab's URL to popup
appAPI.message.toPopup({
type: 'active-tab-url',
url:encodeURIComponent(location.href)
});
});
// THE REST OF YOUR CODE
});
activeTabUrl の値が更新されません。NULL値が与えられます。追伸:背景.jsとポップアップの間で通信できます。しかし、何らかの理由で appAPI.message.toActiveTab 関数が機能しません。私がどこで間違いを犯しているのでしょうか?
背景.js (編集)
var tabUrl='';
/* appAPI.tabs.getActive(function(tabInfo) {
tabUrl = tabInfo.tabUrl;
}); */
appAPI.message.addListener(function(msg) {
appAPI.tabs.getActive(function(tabInfo) {
tabUrl = tabInfo.tabUrl;
});
var dataString = '{"url":"'+tabUrl+'","access":"'+msg.access+'","toread":"'+msg.toread+'","comment":"'+msg.comment+'"}';
alert(dataString);
appAPI.request.post({
url: 'REST API URL',
postData: dataString,
onSuccess: function(response, additionalInfo) {
var details = {};
details.response = response;
appAPI.message.toPopup({
response:response
});
},
onFailure: function(httpCode) {
// alert('POST:: Request failed. HTTP Code: ' + httpCode);
}
});
});
Background.js の動作コード
appAPI.message.addListener(function(msg) {
appAPI.tabs.getActive(function(tabInfo) {
var dataString = '{"url":"'+tabInfo.tabUrl+'","access":"'+msg.access+'","toread":"'+msg.toread+'","comment":"'+msg.comment+'"}';
// alert(dataString);
appAPI.request.post({
url: 'http://fostergem.com/api/bookmark',
postData: dataString,
onSuccess: function(response, additionalInfo) {
var details = {};
details.response = response;
appAPI.message.toPopup({
response:response
});
},
onFailure: function(httpCode) {
// alert('POST:: Request failed. HTTP Code: ' + httpCode);
}
});
});
});
解決
このコードサンプルでは、 アクティブタブURL 変数は、から応答を受信した場合にのみ設定されます。 拡張子.js メッセージングは設計上非同期であるため、ファイルに保存されます。したがって、電話をかけるときは、 alert(activeTabUrl);
コードでは、メッセージはまだ受信されていません。 拡張子.js したがって、値は初期化されたままの null のままです。
を使用するには、 アクティブタブURL 変数からのメッセージを待つ必要があります。 拡張子.js ファイルなので、変数を使用してコードをメッセージ リスナーのコールバックに (できれば関数として) 配置する必要があります。また、ポップアップ コードでアラートを使用するとポップアップが閉じてしまうため、ポップアップ スコープでは使用しないでください。
次のポップアップ コードをテストしました。混乱を避けるために変数を削除し、アクティブなタブの URL をパラメータとしてメッセージ リスナーで呼び出される関数に渡します。これは期待どおりに機能しました。
function crossriderMain($) {
// Message listener for response from active tab
appAPI.message.addListener(function(msg) {
if (msg.type === 'active-tab-url') ShowPageUrl(msg.url);
});
function ShowPageUrl(url) {
$('#page-url').html('<b>Page URL</b>: ' + url);
}
// Request URL from active tab
appAPI.message.toActiveTab({type: 'active-tab-url'});
//alert(activeTabUrl);
// THE REST OF YOUR CODE
}
[免責事項:クロスライダー社員です】