-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbg.js
149 lines (139 loc) · 3.46 KB
/
bg.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// TODO: Dont work if not login
chrome.commands.onCommand.addListener(function(command) {
/*
* Highlight text and save it on server
*/
if (command == "highlight-selected-text"){
getKey("token", function(token){
if (token == null){
return;
}
getCurrentTab(function(tab){
// send message to tab to get text
chrome.tabs.sendMessage(tab.id, {method: "get_selected_text"}, function(resp){
// prepare to save
if (resp.data === "") {
return;
}
console.log(resp.data)
showHighlightPopup(tab, resp.data)
})
})
})
}
})
/*
* Message Listeners
*/
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
/*
* Save Highlight after editing
*/
if (message.method == "save_highlight"){
console.log(message)
var parentTab = message.parentTab
// delete extra keys
delete message.method
delete message.parentTab
// save and highlight
annotateText(message, parentTab)
chrome.tabs.sendMessage(
parentTab.id,
{method: "highlight_text", text: message.text}
)
}
})
/*
* Show popup menu to edit highlight and add attributes
*/
function showHighlightPopup(parentTab, text){
// Make window configuration
var cfg = {
url: 'components/highlight_edit.html',
type: 'popup',
focused: true,
width: 400,
height: 400
}
// Create window
chrome.windows.create(cfg, function (window) {
var tab = window.tabs[0]
// fill form call
var fillTab = function () {
var data = {
method: "highlight_edit",
parentTab: parentTab,
text: text
}
chrome.tabs.sendMessage(tab.id, data)
}
// send fill form call
if (tab.status == "complete"){
fillTab()
} else {
// TODO: ugly hack
setTimeout(fillTab, 600)
}
})
}
/*
* Given the text and other things, save it to server
*/
function annotateText(hlData, tab){
var data = JSON.stringify(hlData)
console.log(data)
// request function
var request = function(token){
$.ajax({
type: "POST",
url: 'http://anotode.herokuapp.com/api/highlights?token=' + token,
contentType: 'application/json; charset=utf-8',
data: data,
success: function(data, textStatus, jqXHR){
console.log(data)
consoleLogContent("Highlighted text saved")
},
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus + " " + errorThrown)
},
dataType: 'json'
})
}
// get token and do request
getKey("token", function(token){
request(token)
})
}
/*
* CONTEXT MENUS
*/
chrome.contextMenus.create({
id: "anotode-annotate",
title: "Anotode: Annotate",
contexts: ["selection"]
})
chrome.contextMenus.onClicked.addListener(function(info, tab){
if (info.menuItemId == "anotode-annotate"){
console.log(info.selectionText)
getKey("token", function(token){
if (token != null){
showHighlightPopup(tab, info.selectionText)
}
})
}
})
// http://stackoverflow.com/questions/7303452/how-to-get-current-tabid-from-background-page
function getCurrentTab(tabCallback) {
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) {
tabCallback(tabArray[0])
}
)
}
// console log content script
function consoleLogContent(msg){
getCurrentTab(function(tab){
chrome.tabs.sendMessage(tab.id, {method: "console_log", data: "MSG: " + msg})
})
}