Skip to content

Instantly share code, notes, and snippets.

@bennettmcelwee
Created July 16, 2013 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennettmcelwee/6006137 to your computer and use it in GitHub Desktop.
Save bennettmcelwee/6006137 to your computer and use it in GitHub Desktop.
Chrome extension to allow lookup of books in Amazon and Fishpond
// Copyright (c) 2013 Bennett McElwee. All rights reserved.
// This is a Chrome extension.
// The onClicked callback function.
function onClickHandler(info, tab) {
// console.log("Info: " + JSON.stringify(info));
if (info.menuItemId == "bookup-amazon-to-fishpond") {
var isbn = getAmazonIsbn(info.pageUrl);
chrome.tabs.update(tab.id, {url: getFishpondUrl(isbn)});
} else if (info.menuItemId == "bookup-fishpond-to-amazon") {
var isbn = getFishpondIsbn(info.pageUrl);
chrome.tabs.update(tab.id, {url: getAmazonUrl(isbn)});
} else if (info.menuItemId == "bookup-amazon") {
var isbn = info.selectionText.trim();
chrome.tabs.update(tab.id, {url: getFishpondUrl(isbn)});
} else if (info.menuItemId == "bookup-fishpond") {
var isbn = info.selectionText.trim();
chrome.tabs.update(tab.id, {url: getFishpondUrl(isbn)});
}
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
function getFishpondUrl(isbn) {
return "http://www.fishpond.co.nz/advanced_search_result.php?isbn=" + isbn;
}
function getAmazonUrl(isbn) {
return "http://www.amazon.com/gp/product/" + isbn;
}
function getAmazonIsbn(url) {
var match = url.match(/\/gp\/product\/(\d+)/);
return match ? match[1] : '';
}
function getFishpondIsbn(url) {
var match = url.match(/.*\/(\d+)\/?$/);
return match ? match[1] : '';
}
// Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
// chrome.contextMenus.create({
// "documentUrlPatterns": ["*://*.fishpond.co.nz/Books/*"],
// "title": "Look up this book on Amazon",
// "contexts":["page"],
// "id": "bookup-fishpond-to-amazon"
// });
chrome.contextMenus.create({
"documentUrlPatterns": ["*://*.amazon.com/gp/product/*"],
"title": "Look up this book on Fishpond",
"contexts":["page"],
"id": "bookup-amazon-to-fishpond"
});
chrome.contextMenus.create({
"title": "Look up %s on Amazon",
"contexts":["selection"],
"id": "bookup-amazon"
});
chrome.contextMenus.create({
"title": "Look up %s on Fishpond",
"contexts":["selection"],
"id": "bookup-fishpond"
});
});
{
"name": "Bookup",
"description": "Look up a book",
"version": "0.1",
"permissions": ["contextMenus"],
"background": {
"persistent": false,
"scripts": ["bookup.js"]
},
"manifest_version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment