Google Chrome Extension - waiting until page loads

前端 未结 2 1173
面向向阳花
面向向阳花 2021-01-01 13:58

In my Google Chrome Extension, I have a Content Script (content.js) and a Background Page (background.html). I have context.js checking for a keyword that appears on the pag

相关标签:
2条回答
  • 2021-01-01 14:03

    As an alternative to manifest's "run_at": "document_end", there could be applied a standard javascript event handling approach. For example, DOMContentLoaded event could be used to run the logic that requires loaded DOM.

    manifest.json

    "content_scripts": [
            "js": ["content.js"],
            "run_at": "document_start"
        }
    ]
    

    content.js

    console.log('The extension works');
    // ... logic that does not need DOM
    
    function run() {
      console.log('The DOM is loaded');
      // ... logic that needs DOM
    }
    
    document.addEventListener("DOMContentLoaded", run);
    
    0 讨论(0)
  • 2021-01-01 14:25

    Try to add this to the "content_scripts" part of your manifest.json file.

    "run_at": "document_end"
    

    http://code.google.com/chrome/extensions/content_scripts.html

    0 讨论(0)
提交回复
热议问题