How to retrieve the element where a contextmenu has been executed How to retrieve the element where a contextmenu has been executed javascript javascript

How to retrieve the element where a contextmenu has been executed


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

manifest.json

"content_scripts": [{  "matches": ["<all_urls>"],  "js": ["content.js"],  "all_frames": true,  "match_about_blank": true}]

content script.js

//content scriptvar 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

//backgroundfunction mycallback(info, tab) {    chrome.tabs.sendMessage(tab.id, "getClickedEl", {frameId: info.frameId}, data => {        elt.value = data.value;    });}