[過去ログ] JavaScript の質問用スレッド vol.125 (1002レス)
上下前次1-新
抽出解除 レス栞 あぼーん
このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
767(1): デフォルトの名無しさん (ワッチョイ a3ad-a1qf [210.250.191.46]) [sage] 2023/03/20(月) 19:47:12.99 ID:PTBUy+ct0(1/4) AAS
chromeの拡張機能で、「ngWord.json」内のデータを「background.js」経由で「content_script.js」に格納しようとしていたのですが、上手くいきません。コードでおかしな点等あれば、ご教授頂けないでしょうか。
【background.js】
fetch('ngWord.json')
.then(response => response.json())
.then(data => {
// content_script.jsにデータを送信
chrome.runtime.sendMessage({ngWords: data});
});
【content_script.js】
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.ngWords) {
const ngWords = message.ngWords;
console.log(ngWords); // 受信したデータを表示
}
});
769(1): デフォルトの名無しさん (ワッチョイ a3ad-a1qf [210.250.191.46]) [sage] 2023/03/20(月) 20:37:35.10 ID:PTBUy+ct0(2/4) AAS
>>768「タブ」というのは、「content_script.js」のことでしょうか?
だとすると「chrome.tabs.sendMessage(tabId: content_script.js, ngWords: data)のように書く感じでしょうか。
774: デフォルトの名無しさん (ワッチョイ a3ad-a1qf [210.250.191.46]) [sage] 2023/03/20(月) 21:53:15.40 ID:PTBUy+ct0(3/4) AAS
【background.js】
fetch('ngWord.json')
.then(response => response.json())
.then(data => {
chrome.tabs.query({active:true,(tabs)=>{
chrome.tabs.sendMessage(tabs[0].id,{ngWords:data})
});
};
【content_script.js】
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.ngWords) {
const ngWords = message.ngWords;
console.log(ngWords);
}
});
このように修正しましたが、上手く取得できないようです。
777(1): デフォルトの名無しさん (ワッチョイ a3ad-a1qf [210.250.191.46]) [sage] 2023/03/20(月) 22:24:56.04 ID:PTBUy+ct0(4/4) AAS
申し訳ありません、私の知識不足で、よく分かりません。もう一度格闘して、改めて質問させて頂きます。ご教授下さり、ありがとうございます。
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.048s