Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active February 2, 2023 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdalon/c6e79354f56958241423256be1217e04 to your computer and use it in GitHub Desktop.
Save tdalon/c6e79354f56958241423256be1217e04 to your computer and use it in GitHub Desktop.
Chrome Extension for Goodreads (Quick Edit, Amazon to Goodreads)
// Commands
chrome.commands.onCommand.addListener(function (command) {
if (command === "az2gr") {
Amazon2Goodreads();
} // end if command share2teams
}); // end command.addListener
function CrxHelp(){
// Open CRX Help
window.open("https://"); // TODO
}
function CrxRn(){
// Open Crx Release Notes
window.open("https://"); // TODO
}
function Amazon2Goodreads(){
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
url = tabs[0].url;
re = /amazon\..*\/dp\/([A-Z0-9]*)/;
var found = url.match(re);
if (found == null) {
alert('Sorry, this doesn\'t appear to be an Amazon book page.');
return;
}
window.open('https://www.goodreads.com/search?q='+ found[1]);
});
} // eofun Amazon2Goodreads
function GoodreadsEdit(){
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
url = tabs[0].url;
re = /goodreads\.com\/book\/show\/([A-Z0-9]*)/;
var found = url.match(re);
if (found == null) {
alert('Sorry, this doesn\'t appear to be a Goodreads book page.');
return;
}
window.open('https://www.goodreads.com/review/edit/'+ found[1]);
});
} // eofun GoodreadsEdit
// Context menus
// Top level Number limited to 6
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
id: "my_crx_help",
title: "Help",
contexts: ["browser_action"]
});
/* chrome.contextMenus.create({
id: "my_crx_rn",
title: "Release Notes",
contexts: ["browser_action"]
}); */
// Add separator
chrome.contextMenus.create({
id:"nws_crx_sep1",
type: "separator",
contexts: ["browser_action"]
});
// Goodreads
chrome.contextMenus.create({
title: "Goodreads",
id: "goodreads",
contexts:["browser_action"]
});
chrome.contextMenus.create({
id: "my_crx_az2gr",
parentId:"goodreads",
title: "Amazon to Goodreads",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
id: "my_crx_gre",
parentId:"goodreads",
title: "Goodreads Edit Book",
contexts: ["browser_action"]
});
// Add Context Menu listener
chrome.contextMenus.onClicked.addListener(function(info, tab) {
switch (info.menuItemId) {
case "my_crx_help": // Goodreads Amazon to Goodreads
CrxHelp();
return;
case "my_crx_rn": // Goodreads Amazon to Goodreads
CrxRn();
return;
case "my_crx_az2gr":
Amazon2Goodreads();
return;
case "my_crx_gre":
GoodreadsEdit();
return;
default:
return
} // end switch
});
// #START
chrome.omnibox.onInputEntered.addListener(
function (searchStr) {
// if user enters a keyword after the omnibox keyword, redirect search to different destination
var splitText = searchStr.split(' ');
var firstWord = splitText[0];
if (firstWord == "-h") {
CrxHelp();
return
}
if (firstWord == "-r") {
CrxRn();
return
}
switch (firstWord.toLowerCase()) {
case 'gr+': // Goodreads Amazon to Goodreads
closeCurrentTab();
Amazon2Goodreads();
return;
case 'gr':
searchStr = searchStr.substring(firstWord.length + 1);
if (searchStr == null) {
sUrl = "https://www.goodreads.com" ;
} else {
searchGoodreads(searchStr);
}
chrome.tabs.update({ url: sUrl });
return;
default:
return
} // end switch
chrome.tabs.update({ url: sUrl });
});
// -------------------------------------------------------------------
function searchGoodreads(searchStr){
sSearchRootUrl = "https://www.goodreads.com/review/list/4083745-thierry-dalon?"
patt = /#[^\s#]*/g;
arrMatch = searchStr.match(patt);
var sTags;
if (arrMatch !== null){
for (var i= 0;i<arrMatch.length;i++){
var tag=arrMatch[i];
tag=tag.slice(1); // remove trailing #
if (i===0) {
sTags = "&shelf=" + tag;
} else {
sTags= sTags + "%2C" + tag;
}
}// end for tag array
searchStr = searchStr.replace(patt,"");
}
searchStr = searchStr.trim();
if (searchStr.length>0){ // search with keywords
sUrl = sSearchRootUrl + "&search%5Bquery%5D=" + searchStr
//sSearch := StrReplace(sSearch," ","+")
//if (sTags !== undefined){
// searchStr = searchStr + "+" + sTags ;
//}
} else {
sUrl = sSearchRootUrl + sTags;
}
chrome.tabs.update({ url: sUrl });
} // end function searchGoodreads
// ----------------------------------------------------------------------------------------------------------
function closeCurrentTab(){
// Close current tab and browser window if last tab
// chrome.tabs.remove will not close the window on last tab closure -> needs to check if single tab opened
chrome.tabs.query({
//active: true,
currentWindow: true
}, function(tabs) {
if (tabs.length > 1) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.remove(tabs[0].id, function() { });
});
} else {
chrome.windows.getCurrent(function(win) {
chrome.windows.remove(win.id, function() { });
});
}
});
} // eofun
{
"name": "My",
"description": "In the Omnibox, type 'm ' followed by TAB, then a Target keyword or full name <space> then a Id Keyword or full name <space> then your search term to instantly search.",
"version": "1.1",
"author": "Thierry Dalon",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"omnibox": { "keyword" : "m" },
"manifest_version": 2,
"permissions": ["tabs","contextMenus","clipboardWrite","https://*/*", "http://*/*"],
"browser_action": {
"default_icon": {
"16": "my_16.png",
"32": "my_32.png"
}
},
"icons": {
"16": "my_16.png",
"32": "my_32.png",
"48": "my_48.png",
"128": "my_128.png"
},
"commands": {
"az2gr" : {
"suggested_key": {
"default": "Alt+G"
},
"description": "Amazon to Goodreads"
}
}
}
@tdalon
Copy link
Author

tdalon commented Jan 18, 2023

This will add a context menu to the CRX icon with entries for Goodreads Quick Edit and Amazon to Goodreads functionality.
Manifest v2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment