Is there any chrome. * API for my google-chrome extension to find out on which pages scripting is forbidden?

I am developing a Google Chrome extension with a script content, a help page, a settings page and a pop-up page. I diligently studied that although the contents of the script are uploaded to the Chrome Web Store, onMessage listeners never call.

I can break sequentially at the point where I execute chrome.extension.onMessage.addListener , but the listener is never called. Therefore, when I execute chrome.tabs.sendMessage in the background or in pop-up pages, the responseCallback function is never called, i.e. The logic inside responseCallback never executed. Chrome does not even consider this as an error scenario - it accepts the request, but then was silent.

If he considered this as an error case (as when trying to send a message to the chrome:// page), then responseCallback would be called with an undefined response argument - and this would cause my error handling code to execute ... but not such luck.

I could add special behavior in my code for http(s)://chrome.google.com/webstore/* , but hoped that there was a more formal way to search for pages that were banned. Any pointer would be appreciated.

I found a great post here , but it does not have a software API, hence this question.

I already know that the Chrome and chrome:// URLs are special cases. However, in my investigation, the behavior on the chrome:// URL is different from the behavior on the Chrome Web Store. On chrome:// urls, my content script doesn't seem to load at all, and the sendMessage responseCallback function is called using the null / undefined response object. This is the official form of the error signaling API, and it works great for me.

My problem is being able to programmatically detect errors like the ones experienced in the Chrome Web Store, without the need for hard-coding URLs (URLs can change over time).

Thanks in advance for your answers.

+4
source share
2 answers

How about adding a listener for chrome.webNavigation.onCompleted in the background script? When a new page loads, send a special β€œping” message to the tab and use chrome.extension.onMessage to receive and respond to ping in your script content. If the content of the script does not respond, you know that it does not work on this page.

+2
source

You can try running chrome.tabs.executeScript to see if you are allowed to create scripts in this domain.

 chrome.tabs.executeScript(tabId, {code: ""}, function() { if(chrome.runtime.lastError) { // Something went wrong, perhaps no rights } else { // Scripting allowed } }); 
0
source

Source: https://habr.com/ru/post/1444240/


All Articles