私はJSが苦手で、コードに表示されていない愚かな問題が発生しています...皆さんが私を助けてくれたら、本当にありがたいです。
私の拡張機能は、現在のタブのURLでいくつかのことを行います。バックグラウンドページでonUpdateイベントを使用し、変数にタブのURLを設定してから、ポップアップで使用しても問題ありませんでした。
重要なのは、ユーザーがURLを更新せずに別のタブを選択し始めた場合、イベントが再度トリガーされることはないということです...したがって、onSelectionChangedイベントもリッスンしています。
問題は、onSelectionChangedイベントのパラメーター内に「tab」オブジェクトがないため、tab.urlプロパティを要求できないことです。
chrome.tabs.getCurrent()メソッドを使おうとしましたが、明らかに何か間違ったことをしています...そして知識の限界に達しました。
これがコードです。皆さんが私を見て正しい方向に向けることができれば、本当に感謝します。
<script>
var tabURL = '';
var defaultURLRecognition = [ "test" ];
// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
//THIS IS WHAT'S NOT WORKING, I SUPPOSE
if (tab==undefined) {
chrome.tabs.getCurrent(function(tabAux) {
test = tabAux;
});
}
//
// If there's no URLRecognition value, I set the default one
if (localStorage["URLRecognition"]==undefined) {
localStorage["URLRecognition"] = defaultURLRecognition;
};
// Look for URLRecognition value within the tab's URL
if (tab.url.indexOf(localStorage["URLRecognition"]) > -1) {
// ... show the page action.
chrome.pageAction.show(tabId);
tabURL = tab.url;
}
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
// Listen for tab selection changes
chrome.tabs.onSelectionChanged.addListener(checkForValidUrl);
</script>