Source Code
manifest.json
{
"name": "MY First Extension",
"description": "Shows a menu item",
"version": "0.3",
"permissions": ["contextMenus", "activeTab", "downloads"],
"icons": {
"16": "16.png",
"48": "48.png",
"64": "64.png",
"128": "128.png"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://www.google.com/*", "*://www.youtube.com/*"],
"js": ["index.js"]
}
],
"manifest_version": 2
}
index.js
console.log("My extension")
var links = document.getElementsByTagName("a");
var formatted_links = [];
for(let i = 0; i < links.length; i++){
let title = links[i].text;
let href = links[i].href;
if(title !== "" && href !== ""){
formatted_links.push({title: title, href: href});
}
}
chrome.runtime.sendMessage({
"action": "print_messages",
"data": formatted_links
}, res => {
console.log(res);
})
background.js
function MyImageClick(info, tab){
console.log("Clicked an image", info, tab);
chrome.downloads.download({"url": info.srcUrl})}
function MyQuoteClick(info, tab){
console.log("Clicked an image", info, tab);
chrome.downloads.download(info.srcUrl)}
chrome.contextMenus.create({
"title": "Share Image",
"contexts": ["image"],
"onclick": MyImageClick})
chrome.contextMenus.create({
"title": "Share Quote",
"contexts": ["selection"],
"onclick": MyQuoteClick})
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse){
console.log("message", msg)
sendResponse({"text": "Received the links"});})