0

I am creating a contextMenu (you right click on some content on a webpage and use it) using the Chrome Extension.

It is built this way:

var menuItem = {
    "id": "Wikit",
    "title": "Wikit",
    "contexts": ["selection"]
};

chrome.contextMenus.create(menuItem);


    url = 'http://localhost:5000/api/v1/players/' + fixedEncodeURI(clickData.selectionText)
    return new Promise((reslove, reject) => {
        fetch(url, myInit)
            .then(response => response.json())
            .then(responseText => {
                var popup_url = 'popup.html'
                var createData = {
                    "url": popup_url,
                    "type": "popup",
                    "top": 5,
                    "left": 5,
                    "width": 500,
                    "height": 500
                };          
                chrome.windows.create(createData, function(){});  
                console.log(responseText)
            ...// more info hidden

Whenever I right click and click on the extension icon, it pops up the popup.html as I intended. But I want to do more complicated tasks by passing down variables and using them in that popup.html.

What is a good way of doing this?

Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • You should take a look at [message passing in chrome extensions](https://developers.chrome.com/extensions/messaging) – Titus Jun 29 '19 at 07:19
  • Possible duplicate of [Passing message from background.js to popup.js](https://stackoverflow.com/questions/12265403/passing-message-from-background-js-to-popup-js) Assuming the code you wrote above is in a background page, which I believe is the right way to create context menu items as compared to creating them in content script. – Gaurang Tandon Jun 29 '19 at 07:27
  • @GaurangTandon does the popup has to be a `.js` file instead of an `html`? – Dawn17 Jun 29 '19 at 07:29
  • @Dawn17 You need to have js code to be able to receive message sent from background page. The js code can be placed in the html file like you do for a normal webpage (script src="", or self-contained) – Gaurang Tandon Jun 29 '19 at 07:59
  • See [Modify a tab with an extension html file inside when opened from popup](//stackoverflow.com/a/54715122) – wOxxOm Jun 29 '19 at 15:47

0 Answers0