Since Chrome does not allow you to interact with the element that you clicked using the context menu, you need to create a script content that stores the last element that was right-clicked on the page, so when the user right-clicks on any element, you can use it.
First you need to create a save_last_element.js content script, for example:
var LAST_SELECTION, LAST_ELEMENT; document.body.addEventListener('contextmenu', function(e) { LAST_SELECTION = window.getSelection(); LAST_ELEMENT = e.target;
Then you add it to your manifest.json :
"permissions": ["*://*/*"], // don't forget to set the permissions for all the pages "content_scripts": [ { "matches": ["*://*/*"], "js": ["/path/to/save_last_element.js"], "run_at": "document_idle", "all_frames": true } ]
Now when you enter the script on the page, you can use the LAST_SELECTION and LAST_ELEMENT variables to refer to the last element with a right click and edit its CSS or whatever.
In your background.js you should do something like this:
function handler(info, tab) { // here you can inject a script inside the page to do what you want chrome.tabs.executeScript(tab.id, {file: '/path/to/script.js', all_frames: true}); } var myContextMenuItem = chrome.contextMenus.create({ "title": "Some title", "contexts": ["all"], "documentUrlPatterns": ["*://*/*"], "onclick": handler });
And finally, inside your script.js file:
if (LAST_SELECTION) {