You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for some unknown reasons it stopped working that way months ago and i desperately need to use it asynchronously!
the solution i tried looks something like this:
i created a utility function in another file utils.js to use it anywhere i needed
exportfunctionsendMessageToContent(tabId,request){returnnewPromise((resolve,reject)=>{chrome.tabs.sendMessage(tabId,request,(response)=>{console.log("response recieved from content script -- ",response,"at ",newDate().toISOString());if(chrome.runtime.lastError){console.log("error caught in response of content script -- ",chrome.runtime.lastError);reject(chrome.runtime.lastError);}else{resolve(response);}});});}
and i use it in my popup.jsx like this:
// I call this function on a button clickconstgetCanonicalLink=async()=>{const[tab]=awaitchrome.tabs.query({currentWindow: true,active: true,});console.log("message sent for tab id -- ",tab.id)constresponse=awaitsendMessageToContent(tab.id,{type:'get_data'});console.log("response -- ",response);returndata;};
The thing is i am not getting a response from the sendMessageToContent and instead the chrome.runtime.lastError is set in callback of this function and this following error is caught:
as per the code in my content script (mentioned below) the code is executed successfully and there is no error in the logic but there is always a delay between the last log of the content script and the callback in the sendMessageToContent
content script log:
popup log:
afaik, the call back is called only when the sendResponse is called, so i guess the flow is OK. but why am i getting the response in this callback as undefined
This is how far i have reached and i cant understand what shall i do exactly to make it work asynchronously!
BTW, here is the code in content script:
chrome.runtime.onMessage.addListener(asyncfunction(request,sender,sendResponse){console.log('request',request);console.log('sender',sender);if(request.type==='get_data'){try{constab=awaitfetch(window.location.href);letparser1=newDOMParser();// Parse the textlethtml=parser1.parseFromString(awaitab.text(),'text/html');console.log(html);// let canonical = html.querySelector('link[rel=canonical]')['href'];letcanonical=html.querySelector('link[rel=canonical]');console.log('canonical',canonical);if(canonical){canonical=html.querySelector('link[rel=canonical]')['href'];letusername=document.querySelectorAll('#channel-handle')[1].textContent;console.log('canonical',canonical);// return canonical;sendResponse({canonicalLink: canonical,redirectedUrl: ab.url,
username,});}else{sendResponse({canonicalLink: null,redirectedUrl: ab.url,username: window.location.href.split('@')[1],});}console.log("request processed!!",newDate().toISOString());}catch(error){console.log("error caught in content script -- ",error);sendResponse({error})}// return true;}}
The text was updated successfully, but these errors were encountered:
Isnt this the same as https://stackoverflow.com/a/20077854/3711660?
Meaning you need to return true and then use async code like await fetch(). Wrap async code in a function call it (with void operator, not await like soe void runAsyncCode();return true; )and return true
I used to add await directly while calling the
chrome.tabs.sendMessage
and it used to work great:for some unknown reasons it stopped working that way months ago and i desperately need to use it asynchronously!
the solution i tried looks something like this:
i created a utility function in another file
utils.js
to use it anywhere i neededand i use it in my popup.jsx like this:
The thing is i am not getting a response from the
sendMessageToContent
and instead thechrome.runtime.lastError
is set in callback of this function and this following error is caught:as per the code in my content script (mentioned below) the code is executed successfully and there is no error in the logic but there is always a delay between the last log of the content script and the callback in the
sendMessageToContent
content script log:
popup log:
afaik, the call back is called only when the
sendResponse
is called, so i guess the flow is OK. but why am i getting the response in this callback asundefined
This is how far i have reached and i cant understand what shall i do exactly to make it work asynchronously!
BTW, here is the code in content script:
The text was updated successfully, but these errors were encountered: