Skip to content

Instantly share code, notes, and snippets.

@ymkjp
Created November 26, 2012 09:54
Show Gist options
  • Save ymkjp/4147446 to your computer and use it in GitHub Desktop.
Save ymkjp/4147446 to your computer and use it in GitHub Desktop.
To test closure
(function () {
"use strict";
var ContentScripts = (function () {
function isBook(category) {
if (category.value === "books") {
return true;
}
return false;
}
function haveAlreadyKindleEdition() {
if (document.getElementById("kindle_meta_binding_winner")) {
return true;
}
return false;
}
function getTargetUrl(asin) {
return 'http://www.amazon.co.jp/gp/digital/fiona/detail/request-kindle-edition/ref=dtp_dp_su_' + asin.value + '?ie=UTF8&a=' + asin.value;
}
function sendRequest(targetUrl) {
// XXX: test
alert(001);
var xhr = new XMLHttpRequest();
xhr.open("GET", targetUrl, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
//writeResult(targetUrl);
// TODO: return xhr.responseText
return true;
}
};
xhr.send();
// TODO: return false;
}
function writeResult(targetUrl) {
// XXX: test
alert(002);
var title = document.getElementById("btAsinTitle") || '',
tgtEle = document.getElementsByClassName("fionaPublish") || false;
if (!tgtEle) {
return false;
}
tgtEle[0].innerHTML += "<span style=\"color: #999; background-color: #FFF; font-size: medium;\"><p><b>Kindlize It</b>: 『<a href=" + targetUrl + ">" + title.innerHTML + "</a>』の Kindle 化リクエストを送信しました。</p></span>";
}
var category = document.getElementById('storeID') || false,
asin = document.getElementById('ASIN') || false;
return {
isBook: function(category) { return isBook(category); },
haveAlreadyKindleEdition: function() { return haveAlreadyKindleEdition(); },
getTargetUrl: function(asin) { return getTargetUrl(asin); },
sendRequest: function(targetUrl) { return sendRequest(targetUrl); },
writeResult: function(targetUrl) { return writeResult(targetUrl); },
category: category,
asin: asin,
}
}());
(function () {
var category = ContentScripts.category,
asin = ContentScripts.asin,
isBook = ContentScripts.isBook(category),
haveAlreadyKindleEdition = ContentScripts.haveAlreadyKindleEdition(),
targetUrl = ContentScripts.getTargetUrl(asin);
// 例えばここで
// requestResult = ContentScripts.sendRequest(targetUrl);
// とかやっちゃうとリクエストを減らすという意図が崩れちゃうよね
if (!category || !asin || !isBook || haveAlreadyKindleEdition) {
return false;
}
if (!ContentScripts.sendRequest(targetUrl)) {
return false;
}
ContentScripts.writeResult(targetUrl);
}());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment