-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
139 lines (115 loc) · 3.77 KB
/
popup.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
(function _yourlsExtension() {
var updateSource = function(msg) {
var target = document.getElementById('source_url');
while (target.firstChild)
target.removeChild(target.firstChild);
target.appendChild(document.createTextNode(msg));
};
var updateResult = function(msg, placeholder, docopy) {
var element = document.getElementById('result_url');
if (element) {
element.value = msg;
element.placeholder = placeholder;
if (docopy) {
element.select();
document.execCommand('copy');
}
}
};
var updateError = function(error, errsupp) {
var title = document.getElementById('message_title');
var supp = document.getElementById('message_supp');
title.textContent = error;
supp.textContent = "";
if (errsupp)
injectSupplemental (supp, errsupp);
};
var shorten = function (settings, long_url, keyword) {
var options = {
action: 'shorturl',
format: 'simple',
url: long_url,
signature: settings.signature
};
if (keyword && keyword.length > 1)
options.keyword = keyword;
updateResult("", 'Contacting server...');
browser.runtime.sendMessage({method: "shortenLink", url: long_url, keyword: keyword}).then (function (response)
{
if (response.url) {
updateResult (response.url, "", settings.copy);
}
if (response.error) {
updateError (response.error, response.supp);
}
}, function (error) {updateError ("Communication error within the extension!", communicationErrorMsg);});
}
var _gotSettings = function(settings) {
if (settings.api && settings.signature) {
var _haveTab = function(tabs) {
browser.runtime.sendMessage({method: "getLinkTarget"}).then (function (response) {
var long_url = tabs[0].url;
if (response.linkTarget) {
long_url = response.linkTarget;
}
updateSource(long_url);
document.getElementById('admin').addEventListener(
'click',
function(se) {
window.open(settings.api + "admin/");
}
);
if (settings.keyword) {
browser.runtime.sendMessage({method: "getSelection"}).then (function (response) {
document.getElementById('keyword').value = response.selection;
}, function (error) {updateError ("Communication error within the extension!", communicationErrorMsg);});
updateResult("", "Waiting for keyword...");
document.getElementById('keyword_submit').addEventListener(
'click',
function(se) {
shorten (settings, long_url, document.getElementById('keyword').value);
}
);
} else {
var keywordrow = document.getElementById('keyword_row');
keywordrow.parentNode.removeChild(keywordrow);
updateResult("", "Working...");
shorten (settings, long_url);
}
}, function (error) {updateError ("Communication error within the extension!", communicationErrorMsg);});
};
var _tabQueryError = function(error) {
updateSource('Cannot get current tab URL!');
updateResult('Error:' + error.message, "");
};
browser.tabs.query({active: true, currentWindow: true}).then(_haveTab, _tabQueryError);
} else {
updateSource('Extension not configured');
updateResult('Go to about:addons', "");
}
};
var _optionsError = function(err) {
updateSource('Options not set');
updateResult(err.message);
}
document.getElementById('result_url').addEventListener(
'select',
function(se) {
document.execCommand('Copy');
}
);
document.getElementById('result_copy').addEventListener(
'click',
function(se) {
document.getElementById('result_url').select();
document.execCommand('copy');
}
);
document.getElementById('settings').addEventListener(
'click',
function(se) {
browser.runtime.openOptionsPage();
}
);
browser.storage.local.get().then(_gotSettings, _optionsError);
})();