How would I append functions to the right click menu in the browser? E.g something
appended to the right click menu which does function dosomething()
which is located in my extension.
To do this, we will use two properties pageX and pageY of the mouse click event which will give us the coordinates where the right button was clicked. We will hide the context menu on the mouse button click if it is already being displayed. JavaScript code: HTML.
Right-click your Google Chrome shortcut and click Properties. Note: You have to put a space between chrome.exe before the code. It's two hyphens, and not a long em dash (–). Click OK, launch Chrome, and you'll see the right-click context menu is back to normal.
To make this HTML menu visible when right-clicking in the browser, we need to listen for contextmenu events. We can bind the event listener directly on the document or we can limit the area that a user can right-click to see the custom context menu. In our example, the area for right-click is the entire body element.
Use the chrome. contextMenus API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.
I made simple extenstion using the contextMenu API - link
Hope this works well as an example.
manifest.json -
{ "manifest_version": 2, ... ... "permissions": [ "contextMenus", "tabs"], ... ... "background": { "page": "background.html", "scripts": ["main.js"] } }
main.js -
searchUrbanDict = function(word){ var query = word.selectionText; chrome.tabs.create({url: "http://www.urbandictionary.com/define.php?term=" + query}); }; chrome.contextMenus.create({ title: "Search in UrbanDictionary", contexts:["selection"], // ContextType onclick: searchUrbanDict // A callback function });
For more information on different context types - link
Found out how, using the contextmenu API https://developer.chrome.com/docs/extensions/reference/contextMenus/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With