firefox addon webextension how to getElementsByClassName in browser.menus.onClicked

28 Views Asked by At

update: solution is at https://github.com/mdn/webextensions-examples/tree/faadfca8ddce0c02cc20c3c261c76a9b50073122/context-menu-copy-link-with-types

html is only accessible in content_scripts . Not accessible in background.js .


I'm trying to create a browser addon in firefox by menus .

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus

I found https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/query but I can't figure out how to access the HTML content of the tab.

Goal is to copy a innerHTML of current tab html element with class 'central-featured-lang lang1' to clipboard.

First the html0 variable need to contain the innerHTML of element with class 'central-featured-lang lang1'.

Problem is that I can't access the current tab html elements. getElementsByClassName is not working in this "context".

browser.menus.onClicked.addListener((info, tab) => {
    let tabs = browser.tabs.query({
        active: true,
        currentWindow: true,
    }); 
    var html0 = tabs.getElementsByClassName('central-featured-lang lang1');
    var mid = info.menuItemId;
    console.log(html0);
    console.log(mid);
    if (mid == "divcopy-link") {
    } 
    var range = document.createRange();
});

I'm getting an error

TypeError: tabs.getElementsByClassName is not a function

I tried browser.tabs.getElementsByClassName('central-featured-lang lang1')[0].innerHTML tabs.getElementsByClassName('central-featured-lang lang1')[0].innerHTML document.getElementsByClassName('central-featured-lang lang1')[0].innerHTML

I'm lost.

0

There are 0 best solutions below