Load JQuery into a Chrome extension?

后端 未结 5 565
鱼传尺愫
鱼传尺愫 2020-11-29 06:39

I\'m attempting to load JQuery into my Chrome extension and make it equal to an object but I\'m wondering how would I go about this? basically I\'d like something like...

5条回答
  •  无人及你
    2020-11-29 07:17

    You can do this from a script in your background_page HTML:

    chrome.browserAction.onClicked.addListener(function(tab) {
      chrome.tabs.executeScript(null, { file: "jquery.min.js" }, function() {
        chrome.tabs.executeScript(null, { file: "content_script.js" });
      });
    });
    

    Then in content_script.js do whatever you like:

      $('#header').hide();
    

    Note that even if the page you've injected into already has jQuery loaded, you're still hitting your own instance of the jQuery object, which is accessing the page via the DOM. (document).

提交回复
热议问题