On page load event in Chrome extensions

前端 未结 1 1728
借酒劲吻你
借酒劲吻你 2020-11-30 23:49

I want to check some values in the content of chrome browser page when it completely loaded like that

if(document.body.innerText.indexOf(\"Cat\") !=-1)


        
相关标签:
1条回答
  • 2020-12-01 00:22

    Register a content script in the manifest file at "run_at": "document_idle" (which is the default) and put your code in the content script file. Then the script will be run when the page is ready.

    If you want to detect from the background page whether a page is completely loaded, use the chrome.webNavigation.onCompleted event and do whatever you want, such as calling chrome.tabs.executeScript to execute a content script. This method could be useful over the previous method if the list of URLs is dynamic or if the URL patterns cannot be described using the match pattern syntax.

    chrome.webNavigation.onCompleted.addListener(function(details) {
        chrome.tabs.executeScript(details.tabId, {
            code: ' if (document.body.innerText.indexOf("Cat") !=-1) {' +
                  '     alert("Cat not found!");' +
                  ' }'
        });
    }, {
        url: [{
            // Runs on example.com, example.net, but also example.foo.com
            hostContains: '.example.'
        }],
    });
    

    The webNavigation and host permissions have to be set in manifest.json, e.g.:

    {
      "name": "Test",
      "version": "1.0",
      "background": { "scripts": ["background.js"] },
      "permissions": [ "webNavigation", "*://*/*" ],
      "manifest_version": 2
    }
    
    0 讨论(0)
提交回复
热议问题