-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
executable file
·209 lines (176 loc) · 6.57 KB
/
background.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Diaspora* Publisher extension.
* Copyright (C) 2015 Vittorio Cuculo <me@vcuculo.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
var podUrl;
var minutes = getMinutes();
var t;
getPod();
if (podUrl == null){
alert(chrome.i18n.getMessage("setPodUrl"));
chrome.tabs.create({url : "options.html"});
}
else if (getOpt("notify") == "true")
checkNotifications();
function checkNotifications(){
if (getOpt("notify") == "true"){
minutes = getMinutes();
t = setTimeout(function(){checkNotifications()}, minutes);
getNotifications();
}
else
resetNotifications();
}
function getNotifications(){
getPod();
$.ajax({
type: 'GET',
dataType: 'json',
url: 'https://' + podUrl + "/notifications.json",
success: function (result) {
showNotifications(result);
},
error: function() {
resetNotifications();
}
});
}
function resetNotifications(){
chrome.browserAction.setBadgeText({text: ''});
}
function showNotifications(result){
var n = 0;
for(var x = 0; result.length > x; x++) {
var unread = result[x][Object.keys(result[x])[0]].unread;
if (unread == true)
n++;
}
if (n > 0)
chrome.browserAction.setBadgeText({text: n.toString()});
else
resetNotifications();
}
function googlurl(url, cb) {
$.ajax({
type: 'POST',
url: "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBga1CKqUOlLGZcOkMZSF-m-Xe1PbM-C4s",
contentType: 'application/json',
data: JSON.stringify({longUrl: url}),
success: function (m) {
var result = null;
try {
result = m.id;
if (typeof result != 'string') result = null;
} catch (e) {
result = null;
}
cb(result);
}
});
}
function tinyurl(url, cb) {
$.ajax({
type: 'GET',
url: "http://tinyurl.com/api-create.php",
data: {url: url},
success: function (result) {
cb(result);
}
});
}
function getPod(){
podUrl = (window.localStorage != null && window.localStorage.podUrl != null) ? window.localStorage.podUrl : null;
}
function getMinutes(){
return (window.localStorage != null && window.localStorage.minutes != null) ? window.localStorage.minutes : "300000";
}
function getOpt(opt){
return (window.localStorage != null && window.localStorage.getItem(opt) != null) ? window.localStorage.getItem(opt) : "true";
}
function postMessage(tab_id, subject, url, selection, image) {
var action_url = podUrl + "/bookmarklet?title=";
if (getOpt("msg") == "true"){
var msg = prompt(chrome.i18n.getMessage("addMsg"), chrome.i18n.getMessage("addMsgText"));
if (msg && msg !== "") {
action_url += encodeURIComponent(msg) + "<br><br>";
}
}
if (subject.length > 0 && getOpt("title") == "true")
if(getOpt("markdown") == "true")
action_url += "**" + encodeURIComponent(subject) + "**";
else
action_url += encodeURIComponent(subject);
if (url.length > 0 && getOpt("url") == "true")
action_url += " • " + encodeURIComponent(url);
// Append the current selection & image to the end of the text message.
if (selection.length > 0)
action_url += " • " + encodeURIComponent(selection);
if (image != null && getOpt("images") == "true")
action_url += " <br> [ ![Image](" + image + ") ](" + encodeURIComponent(url) + ")";
if (getOpt("tags") == "true"){
var tags = prompt(chrome.i18n.getMessage("addTags"), chrome.i18n.getMessage("addTagsText"));
if (tags && tags !== "") {
action_url += " <br>" + encodeURIComponent(tags);
}
}
action_url += " <br><sub>&url=[via Diaspora* Publisher](" + encodeURIComponent("http://goo.gl/tmeFB") + ") -</sub>&v=1&";
if (!window.open(action_url+'noui=1&jump=doclose','diasporav1','location=yes,links=no,scrollbars=no,toolbar=no,width=590,height=450'))
location.href = action_url+'jump=yes';
}
chrome.extension.onConnect.addListener(function(port) {
var tab = port.sender.tab;
// This will get called by the content script we execute in
// the tab as a result of the user pressing the browser action.
port.onMessage.addListener(function(info) {
if (info.type == "sendText"){
var max_length = 1024;
var max_url_length = 25;
if (info.selection.length > max_length)
info.selection = info.selection.substring(0, max_length);
// If is video keep original url, otherwise embedding doesnt work
if (tab.url.length > max_url_length && info.video == false){
if (getOpt("short") == "tiny")
tinyurl(tab.url, function(shorturl) { postMessage(tab.id, info.title, shorturl, info.selection, info.image); });
else if (getOpt("short") == "google")
googlurl(tab.url, function(shorturl) { postMessage(tab.id, info.title, shorturl, info.selection, info.image); });
}else if (info.video == true && getOpt("ytimg") == "false"){
postMessage(tab.id, info.title, tab.url, info.selection, null);
}else if (info.video == true && getOpt("vmimg") == "false"){
postMessage(tab.id, info.title, tab.url, info.selection, null);
}else
postMessage(tab.id, info.title, tab.url, info.selection, info.image);
}
else if (info.type == "getImg")
port.postMessage({img: getOpt("images")});
});
});
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function(tab) {
// Inject needed scripts and css
chrome.tabs.executeScript(null, {file: "js/jquery.min.js"});
chrome.tabs.executeScript(null, {file: "js/bootstrap.min.js"});
chrome.tabs.insertCSS(null, {file: "css/bootstrap.css"});
getPod();
if (podUrl.indexOf("http") == -1)
podUrl = "http://".concat(podUrl);
// We can only inject scripts to find the title on pages loaded with http
// and https so for all other pages, we don't ask for the title.
if (tab.url.indexOf("http:") != 0 && tab.url.indexOf("https:") != 0)
postMessage(tab.id, "", tab.url, "");
else
chrome.tabs.executeScript(null, {file: "content_script.js"});
});