How to check tab load

I need to update something on the tabs, for example, check that the page loads correctly and replace something inside the page. here is my code

// background.js chrome.tabs.onUpdate.addListener(function(tabId, changeInfo, tab){ try{ chrome.tabs.executeScript(tabId, filename); } catch(e) { // 1. when I open a new tab // 1. Error during tabs.executeScript: Unknown error. // 2. when I request a url not arrive-able. // 2. Error during tabs.executeScript: Cannot access contents of url // "data:text/html,chromewebdata". Extension manifest must request // permission to access this host. // but I can't catch these errors, they just appers in background console. } }); 

I am trying to execute a script while loading it, but if the current tab is chrome: // newtab or a page with a chrome error, I cannot do this, but I cannot catch the error.

+6
source share
2 answers

There is no direct way to catch these errors. However, I just created a method to achieve my goals:

  • Use chrome.tabs.onUpdated (using d !).
    This event is fired twice when the page is initialized ( "loading" ) and when the DOM is loaded ( "complete" ).
  • Use chrome.tabs.executeScript(tabId, {file: fileName}, fn_callback);
    The second argument must be an object containing either "file" or "code" . The third argument, a function, is always executed after completion of the script injection.

    Now the real implementation:
    • Execute the contents of the script (using chrome.tabs.executeScript ).
    • In the entered content of the script, define the onMessage event.
    • In the chrome.tabs.executeScript callback function, do the following:
    • Use var exec_error = setTimeout(onError, 100); to delay the execution of your onError method. Select the appropriate small delay (100) and save the link to this timeout in a variable, exec_error .
    • Use chrome.tabs.sendMessage(tabId, func) to send a message to the tab.
    • In the func callback function, add this logic:
    • If an error onMessage , the onMessage event defined in 2. will not be introduced. Therefore, the tab will not respond as expected, and the timeout will not be cleared. As a result, onError will be executed .
      Otherwise, the timeout is cleared, and onSuccess is executed .

Code (e.g. background script):

 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { // This event fires twice: loading, and complete. // If we're loading, terminate function, and wait for "complete" if (changeInfo.status === 'loading') return; // On error and on success, this happens: function onError() { console.log('Error injecting the content script!'); } function onSuccess() { console.log('Successfully injected content script!'); } // Execute scripts chrome.tabs.executeScript(tabId, { file: 'test_access.js' }, function() { // This function always fires *after* the attempt to run the code var exec_error = setTimeout(onError, 100); chrome.tabs.sendMessage(tabId, 'Are you there?', function(yes_no) { if (yes_no === 'Yes') { clearTimeout(exec_error); onSuccess(); } }); }); }); 

test_access.js

 chrome.extension.onMessage.addListener(function(req, sender, respond) { if (req === 'Are you there?') { respond('Yes'); } }); // Rest of your content script logic, eg: alert(location.href); 
+11
source

You cannot load scripts through extensions for pages that have a URL, such as chrome:// . However, you can override some pages completely, but not in the same extension in which you upload scripts to some other pages.

0
source

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


All Articles