popup.htmlに書いた<textarea>にurlが送信されたら別ウィンドウを開いてそのタブのDOMにアクセスしたいです。
しかし受信の段階で躓いています。
//errorメッセージ Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
//popup.js $('#submit').click(function(){ let value = $('textarea').val(); if(value){ chrome.windows.create({url : urls[0], focused : true, top : 0, left : 600}, function(win){ chrome.tabs.query({windowId : win.id}, function(tabs){ let windows = chrome.extension.getViews() windows[0].onload = function(){ chrome.tabs.sendMessage(tabs[0].id, {"run":true}) } }) }) } })
//content scripts chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if(request.run){ $(document).ready(function(){ console.log('recieved') }) } return true; })
chrome.windows.create
した直後にsendmessageするのがダメなのかと思い(Chrome拡張のコールバックはすべて非同期のため),
・chrome.extension.getViews()
で送り先のウィンドウオブジェクトを取得し、loadを待つ
・return trueで同期化する https://qiita.com/Tachibana446/items/ab15021099d54d1209c2
を試しましたが、うまくいきません。何かご存知でしたらよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。