-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
62 lines (54 loc) · 1.75 KB
/
background.js
File metadata and controls
62 lines (54 loc) · 1.75 KB
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
chrome.contextMenus.create({
id: "shortenpriv",
title: "Generate private short URL",
contexts: ["link"]
});
chrome.contextMenus.create({
id: "shortenpub",
title: "Generate public short URL",
contexts: ["link"]
});
var portFromCS;
function connected(p) {
portFromCS = p;
}
browser.runtime.onConnect.addListener(connected);
browser.contextMenus.onClicked.addListener(function(info, tab) {
switch (info.menuItemId) {
case "shortenpriv":
shortenURL(false, info.linkUrl);
break;
case "shortenpub":
shortenURL(true, info.linkUrl);
break;
}
});
function shortenURL(isPublic, orgURL) {
function setCurrentSettings(result) {
if ((!result.polrurl.startsWith("http://") && !result.polrurl.startsWith("https://")) || result.polrapikey == "") {
var openingPage = browser.runtime.openOptionsPage();
return;
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
var publictext = "private";
var shortURL = this.responseText;
portFromCS.postMessage({shortURL: shortURL});
if (isPublic) publictext = "public";
var opt = {
type: "basic",
title: "Polrff short URL",
message: "The "+publictext+" short URL for "+orgURL+"hast been copied to the clipboard\r\n("+shortURL+")",
iconUrl: browser.extension.getURL("icons/icon-32.png")
}
chrome.notifications.create("polrff", opt, function(){});
});
oReq.open("GET", result.polrurl+"/api/v2/action/shorten?key="+result.polrapikey.trim()+"&url="+encodeURIComponent(orgURL)+"&is_secret="+!isPublic);
oReq.send();
}
function onError(error) {
console.log(`Error: ${error}`);
}
var getting = browser.storage.sync.get();
getting.then(setCurrentSettings, onError);
}