Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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