Optionally inject Content Script

前端 未结 3 1824
囚心锁ツ
囚心锁ツ 2020-11-27 17:52

Content Script can be injected programatically or permanently by declaring in Extension manifest file. Programatic injection require host permission, which is generally gran

3条回答
  •  臣服心动
    2020-11-27 18:37

    Since the existing answer is now a few years old, optional injection is now much easier and is described here. It says that to inject a new file conditionally, you can use the following code:

    // The lines I have commented are in the documentation, but the uncommented
    // lines are the important part
    
    //chrome.runtime.onMessage.addListener((message, callback) => {
    //   if (message == “runContentScript”){
    
            chrome.tabs.executeScript({
              file: 'contentScript.js'
            });
    
    //   }
    //});
    

    You will need the Active Tab Permission to do this.

提交回复
热议问题