This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
60 lines (54 loc) · 1.62 KB
/
index.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
const fetch = require("node-fetch");
const clipboardy = require("clipboardy");
const applescript = require("applescript");
require("dotenv").config();
const shorten = async (url, subDomain) => {
let apiKEY;
if (subDomain === "sc") {
apiKEY = process.env.KEY_SC;
} else if (subDomain === "go") {
apiKEY = process.env.KEY_GO;
}
try {
const response = await fetch("https://kutt.it/api/v2/links", {
method: "POST",
body: JSON.stringify({
target: url.toString(),
reuse: true,
domain: `https://${subDomain}.srsh.link/`,
}),
headers: {
"content-type": "application/json",
"X-API-KEY": apiKEY,
},
});
return (await response.json()).link;
} catch (error) {
console.error(error);
}
};
const shortenURL = async (url) => {
const shortLink = new URL(
await shorten(url, url.hostname === "cln.sh" ? "sc" : "go")
);
if (shortLink.protocol === "http:") shortLink.protocol = "https:";
const shortUrl = shortLink.toString();
clipboardy.writeSync(shortUrl);
return shortUrl;
};
const run = async (longLink) => {
if (!longLink.match(/^https?:\/\//)) {
longLink = `http://${longLink}`;
}
let notification;
try {
const url = new URL(longLink);
notification = `display notification "${longLink}" with title "Link Shortened" subtitle "${await shortenURL(
url
)}" sound name "purr"`;
} catch (error) {
notification = `display notification "${longLink}" with title "Invalid Link" subtitle "Make sure link has http or https" sound name "glass"`;
}
applescript.execString(notification);
};
run(clipboardy.readSync());