5

How can I display a modal dialog from the context menu?

I can show a new window from context menu which opens in its own tab:

var menuItem = {
    "id": "rateMenu",
    "title": "Rate Item",
    "contexts": ["all"],
}

chrome.contextMenus.create(menuItem);

chrome.contextMenus.onClicked.addListener(function (clickData) {
    open('index.html');
});

I also know how to show a modal dialog, using bootstrap (for example) on the page itself:

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

In this particular case I want to show a modal dialog that you cannot close unless you press the "close" button.

However, I have no idea how to show the modal popup window straight away from the context menu.

Does anyone have any idea how this can be achieved?

Thank you!

Tanuki
  • 717
  • 1
  • 9
  • 24
  • There's no dedicated API for that. Use content scripts. I'll try to find a duplicate question with suitable answer. – wOxxOm Nov 16 '15 at 13:24

1 Answers1

15

I have done the same. Just use content script to show modal.

Example : When user clicks the context menu item, send message to content script to let it know about it so that it should open a modal.

background.js:

chrome.contextMenus.onClicked.addListener(function (clickData) {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
   chrome.tabs.sendMessage(tabs[0].id, {type: "openModal"});
  });
});

contentscript.js:

 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
            switch (request.type){
                case "openModal":
                    $('#myModal').modal({
                       backdrop: 'static',
                       keyboard: false
                    });
                    break;  
});

Make sure to include necessary css and js files under content_scripts in manifest.json

"content_scripts": [
    {
      "matches": ["https://*/*"],
      "css":["bootstrap.min.css"],
      "js":["jquery.min.js","bootstrap.min.js","contentscript.js"],
      "run_at":"document_end"
    }
  ]

NOTE : Using bootstrap.min.css may conflict with the page UI and it may change it. To avoid this, move your modal and the required js and css files in a separate html file(modal.html). Then inject your iframe into the tab using content script.

var iframe = document.createElement('iframe');
iframe.src = chrome.extension.getURL("modal.html");
iframe.frameBorder = 0;
iframe.id = "myFrame";
$(iframe).hide();//necessary otherwise frame will be visible
$(iframe).appendTo("body");

Remember to add modal.html and all the css and js files that are used inside modal.html like modal.js,bootstrap.min.js,bootstrap.min.css under web_accessible_resources:

web_accessible_resources": ["modal.html","modal.js","bootstrap.min.js","bootstrap.min.css"]

Now you can hide or show your iframe using content script but to show and hide modal, it can be done from inside iframe only. So you would need to pass a message from background to iframe to show the modal.The code above mentioned for contentscript will work for iframe also.

In case you want to hide the iframe, just send message from iframe to contentscipt using window.parent.postMessage().

Example:

modal.js:

 window.parent.postMessage({ type: "hideFrame" }, "*");

contentscript.js:

    window.addEventListener("message", function(event) {
         if (event.data.type == "hideFrame") {
              $("#myFrame").hide();
          }
});
Siddharth
  • 6,966
  • 2
  • 19
  • 34
  • 2
    Wow, what a great answer! Thank you so much, this is exactly what I need! – Tanuki Nov 17 '15 at 21:22
  • 1
    Hi, after trying this solution, I hit a problem. The modal window only seems to open inside an iframe. If I hide it, then nothing shows on the screen at all, and if I show the iframe, then you can only see the modal window opening up inside the small iframe frame at the bottom of the page. Do you have an idea how to show modal window on the main page, while keeping bootstrap.css inside the iframe? – Tanuki Nov 19 '15 at 22:28
  • No that's not possible. You have to keep both css and modal inside the frame. If you want to show the modal on main page then dont use iframe. But as I told earlier, it may conflict with the page UI. Most extensions use iframe when they show some content inside the page. – Siddharth Nov 20 '15 at 07:54
  • What code does the `modal.html` contain? I am unable to make the iframe popup on contextMenu click – shinz4u Apr 24 '21 at 20:41