-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
72 lines (69 loc) · 2.39 KB
/
content.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
function init(){
chrome.runtime.sendMessage({key: "saved_ids",action: "getItem"}, function(response) {
saved_ids = JSON.parse(response);
setup_save_div(saved_ids)
});
}
function setup_save_div(saved_ids) {
$(".pagetop").append(" | ");
$(".pagetop").append("<a href='?saved=true'> saved</a>");
$(".subtext").each(function() {
score_div_id = $(this).find(".score").attr("id")
if(score_div_id){
post_id = parseInt(score_div_id.split("_")[1])
index = $.inArray(post_id, saved_ids)
if(index == -1){
saved = false
text = "save"
}else{
saved = true
text = "unsave"
}
save_element = jQuery('<a/>', {
class: 'save',
text: text,
"data-post-id": post_id,
style: "cursor: pointer;"
});
$(this).append(" | ");
$(this).append(save_element);
}
});
}
$(document).on("click", ".save", function() {
current_element = this
chrome.runtime.sendMessage({key: "saved_ids", action: "getItem"}, function(response) {
saved_ids = JSON.parse(response);
post_id = parseInt($(current_element).data("post-id"))
index = saved_ids.indexOf(post_id)
if (index > -1) {
saved_ids.splice(index, 1);
console.log("saved_ids")
console.log(saved_ids)
$(current_element).text("save")
chrome.runtime.sendMessage({key: post_id, action: "removeItem"}, function(response) {});
}else{
saved_ids.push(post_id)
$(current_element).text("unsave");
tr_parent = $(current_element).parents("tr")[0]
tr_up = $(tr_parent).prev().get(0)
tr_down = $(tr_parent).next().get(0)
post_html = tr_up.outerHTML + tr_parent.outerHTML + tr_down.outerHTML;
chrome.runtime.sendMessage({key: post_id, value: post_html, action: "setItem"}, function(response) {});
}
chrome.runtime.sendMessage({key: "saved_ids", value: JSON.stringify(saved_ids),action: "setItem"}, function(response) {});
});
});
window.onload = init;
if (window.location.href.indexOf("saved=true") > -1){
result = ""
chrome.runtime.sendMessage({key: "saved_ids",action: "getItem"}, function(response) {
saved_ids = JSON.parse(response);
for (var i = saved_ids.length - 1; i >= 0; i--) {
chrome.runtime.sendMessage({key: saved_ids[i],action: "getItem"}, function(response) {
result = result + response
$(".itemList").html(result)
});
}
});
}