Inject HTML into a page from a content script

后端 未结 5 1750
南旧
南旧 2020-11-28 23:35

I am building a Chrome Extension and I have a requirement to overlay a blob of html on top of a few websites. At the moment I am using a JQuery .Get to pull the

5条回答
  •  青春惊慌失措
    2020-11-29 00:18

    Yes, that's possible. Use chrome.extension.getURL to get an absolute URL for the resource. For example:

    Step 1:

    $.get(chrome.extension.getURL('/template.html'), function(data) {
        $(data).appendTo('body');
        // Or if you're using jQuery 1.8+:
        // $($.parseHTML(data)).appendTo('body');
    });
    

    Step 2:

    Register the resource in the manifest under web_accessible_resources: See https://developer.chrome.com/extensions/manifest#web_accessible_resources (Provided by @QFDev)

    Update (10/04/2020)

    chrome.extension.getURL is deprecated since Chrome 58. Use this instead chrome.runtime.getURL

提交回复
热议问题