-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkhl_bookmarklet.js
132 lines (108 loc) · 4.88 KB
/
khl_bookmarklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function(w) {
w.KHL = {
used_asins: [],
offset: null,
totalHighlights: 0,
imported: [],
nextBookUrl: "https://kindle.amazon.com/your_highlights/next_book",
getNextBook: function() {
var _this = this;
var urldata = {};
if (_this.offset !== null) { //null for the first request --> don't need to send any params
urldata = {
"used_asins[]": _this.used_asins,
"current_offset": _this.offset
}
}
$.get(_this.nextBookUrl, urldata, function(src) {
_this.process(src);
}, "html");
},
process: function(src) {
var _this = this;
if (src === "") {
_this.end();
return true;
}
var $page = $(src);
var book = $page.filter('.bookMain')[0];
var title = $(book).find('.title a')[0];
title = $(title).text();
var id = $(book).attr("id").split("_");
var asin = id[0];
_this.offset = id[1];
_this.used_asins.push(asin);
var highlights = $page.filter('.yourHighlight');
var highlightedText = [];
$.each(highlights, function(n) {
var loc = $(highlights[n]).find('a.readMore').attr('href');
var kindleLoc = loc.match(/location=(\d+)/)[1];
highlightedText.push({
'content': $(highlights[n]).find('.highlight').text(),
'comment': $(highlights[n]).find('.noteContent').text(),
'kindleLocation': kindleLoc
});
});
var bookdata = {
'asin': asin,
'title': title,
'highlightedText': highlightedText,
'cover': "http://images.amazon.com/images/P/" + asin + ".01.TZZ.jpg",
}
_this.progress(bookdata);
_this.getNextBook();
},
progress: function(bookdata) {
var _this = this;
var coverImg = "http://images.amazon.com/images/P/" + bookdata.asin + ".01.TZZ.jpg";
_this.totalHighlights += bookdata.highlightedText.length;
var word = "highlight";
if(bookdata.highlightedText.length > 1) {
word = word+"s";
}
_this.imported.unshift(bookdata);
$("#cv").html("<img src='" + coverImg + "'>");
$("#cb").html(bookdata.highlightedText.length + " " + word + " from \"" + bookdata.title + "\"");
},
saveData: function (data, fileName) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var json = JSON.stringify(data),
blob = new Blob([json], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
},
end: function() {
var _this = this;
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-" + (currentdate.getMonth()+1) + "-" + currentdate.getDate();
var filename = "kindle_highlights_" + datetime + ".json";
var data = {};
data.totalHighlights = _this.totalHighlights;
data.highlights = _this.imported;
_this.saveData(data, filename);
$("#khl").remove();
delete window.KHL;
// console.log("Highlights import complete!");
},
start: function() {
var _this = this;
if(window.location.href != "https://kindle.amazon.com/your_highlights") {
if(confirm("You must be logged into http://kindle.amazon.com/your_highlights to save your highlights. Go there now?")) {
window.location.href = "https://kindle.amazon.com/your_highlights";
} else {
return false;
}
} else {
_this.getNextBook();
$("body").prepend("<div id='khl' style='position: fixed; width: 100%; height: 120px; background-color: rgba(100, 100, 100, .75); border: 1px solid black; z-index: 1000;'><div style='display: block; position: relative; margin: 0px 0px 0px 50px; height: 100%; line-height: 120px; font-size: 18px; color: white;'><span id='cv' style='display: inline-block; width: 75px; height: 110px; margin: 5px 25px 5px 0px;'></span><span id='cb' style='display: inline-block; position: absolute; margin: auto; height: 100%; line-height: 120px;'>IMPORTING HIGHLIGHTS...</span></div></div>");
// console.log("Highlights import has begun...");
}
}
}
})(window);
KHL.start();