How to retrieve the element where a contextmenu has been executed

后端 未结 1 645
南笙
南笙 2020-11-28 04:53

I am trying to write a google chrome extension where I use a contextmenu. This contextmenu is available on editable elements only (input texts for example). When the context

相关标签:
1条回答
  • 2020-11-28 05:06

    You can inject content script with contextmenu event listener and store element that was clicked:

    content script.js

    //content script
    var clickedEl = null;
    
    document.addEventListener("contextmenu", function(event){
        clickedEl = event.target;
    }, true);
    
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
        if(request == "getClickedEl") {
            sendResponse({value: clickedEl.value});
        }
    });
    

    background.js

    //background
    function mycallback(info, tab) {
        chrome.tabs.sendMessage(tab.id, "getClickedEl", function(clickedEl) {
            elt.value = clickedEl.value;
        });
    }
    
    0 讨论(0)
提交回复
热议问题