Chrome Extension Context Menu: how to append div to page after clicking menu item

后端 未结 1 551
一生所求
一生所求 2020-12-08 17:33

Playing around with building a Chrome extension. At the moment I\'ve put together a context menu item. When the context menu item is clicked, it fires itemClicked()

相关标签:
1条回答
  • 2020-12-08 18:11

    You have probably misunderstood the concept of a background page (and its younger, more resource-friendly and preferred sibling: event page) and that of a content script.

    content scripts:

    • Are bound to a specific web-page loaded into a tab.
    • Live in an isolated world (JS context), but have direct access to the web-pages DOM.
    • Can communicate with the background page (see Message Passing).

    background pages:

    • Are bound to your extension (there is max. 1 background (or event) page for each extension).
    • Are always somewhere in the background (event pages "take a nap" from time to time, but you can always wake them up).
    • Do not have direct access to any web-page.
    • Can communicate with the content scripts (and other views) (see Message Passing).
    • Can do cool stuff (because they have access to cool chrome.* APIs).

    The chrome.contentMenus API is available only to a background page. Thus, you have to create any context menu and listen for onClicked events there (in the background page).
    Once a context menu has been clicked, you can use Programmatic Injection to inject some code (or a content script) into the active tab's web-page.

    Below is the source code of a sample extension that demonstrates this method.

    manifest.json:

    {
        "manifest_version": 2,
        "name":    "Test Extension",
        "version": "0.0",
    
        "background": {
            "persistent": false,   // <-- let's make it an event page
            "scripts": ["background.js"]
        },
    
        "permissions": [
            "contextMenus",
            "activeTab"   // <-- here, sufficient for our purpose
        ]
    }
    

    background.js:

    /* Create a context-menu */
    chrome.contextMenus.create({
        id: "myContextMenu",   // <-- mandatory with event-pages
        title: "Click me",
        contexts: ["all"]
    });
    
    /* Register a listener for the `onClicked` event */
    chrome.contextMenus.onClicked.addListener(function(info, tab) {
        if (tab) {
            /* Create the code to be injected */
            var code = [
                'var d = document.createElement("div");',
                'd.setAttribute("style", "'
                    + 'background-color: red; '
                    + 'width: 100px; '
                    + 'height: 100px; '
                    + 'position: fixed; '
                    + 'top: 70px; '
                    + 'left: 30px; '
                    + 'z-index: 9999; '
                    + '");',
                'document.body.appendChild(d);'
            ].join("\n");
    
            /* Inject the code into the current tab */
            chrome.tabs.executeScript(tab.id, { code: code });
        }
    });
    

    (If your injected code is complicated enough, it might be a better idea to inject a .js file. More info on Programmatic Injection.)

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