0

my initial problem was that I cannot open a chrome extension popup programatically by clicking on my contextMenu, so one workaround is using chrome.tabs.create in combination with chrome.windows.create and it works alright, but I figured if I make a shortcut for the extension, right now it is "Ctrl+Shift+K" combination and the popup opens and I simulate these three key being clicked in my eventHandler for the click on the contextMenu option my popup would open as well, but it has not worked so far.

background.js

chrome.contextMenus.create({
    title: 'Testing',
    contexts: ['selection'],
    'id': 'Test_Selection'
});

chrome.contextMenus.onClicked.addListener(openPopupFromContextMenu);

function openPopupFromContextMenu() {

 let ev = new KeyboardEvent('keypress', {
        key: "k",
        char: "k",
        ctrlKey: true,
        shiftKey: true
     });

     document.dispatchEvent(ev);
}

My shortcut is definitely working correctly and I have tried a thousand different methods of creating the event and dispatching it but it does not work. I tried with deprecated methods as well, because I was quite desperate. So if you have any tips for simulating these three button clicks and successfully dispatching them I'd be glad to hear them.Also I'd prefer pure JavaScript methods of doing so.

I tried something along the lines of

let e = new Event('keydown');
            e.key = 'k';
            e.keyCode = e.key.charCodeAt(0);
            e.which = e.keyCode;
            e.altKey = false;
            e.ctrlKey = true;
            e.shiftKey = true;
            e.metaKey = false;
            e.bubbles = true;
            document.dispatchEvent(e);

but it was not working as well

  • Have you ever tried moving the shortcut simulation logic to content scripts? – Haibara Ai Jul 13 '16 at 09:12
  • 1
    You can't fake a hotkey that's used/set in Chrome's UI for security reasons. I've seen several questions on this topic, I'll try to link a duplicate. – wOxxOm Jul 13 '16 at 09:19
  • @wOxxOm Candidate: https://stackoverflow.com/questions/37993631/trigger-keypress-jquery-in-chrome-extension/ but I can't dupehammer as it's not upvoted/accepted. Would you kindly upvote that one? – Xan Jul 13 '16 at 09:50
  • @Xan I haven't got enough reputation to upvote yet so I'm just a watcher since I'm quite new to the forum and I'm sorry to ask the question since I tried really hard to find a dupe (I have at least 30 tabs opened) but I guess I missed it somehow. Do you think it's possible by moving the logic to content scripts or I should stick with opening a new window with type popup – Georgi Dimitrov Jul 13 '16 at 10:06
  • @HaibaraAi I haven't tried moving the simulation logic to content scripts since I'm not sure if my contextMenu would work from there on all pages, because the goal is to open the popup via the contentScript menu after all. – Georgi Dimitrov Jul 13 '16 at 10:06
  • @GeorgiDimitrov Thanks, but it was directed to wOxxOm. I just gave you the 10 rep required though. It's fine that you didn't find a duplicate - and yours is a well-constructed question, so don't take "it's been asked before" from veterans of this tag personally. – Xan Jul 13 '16 at 10:07

0 Answers0