-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
132 lines (109 loc) · 3.62 KB
/
background.js
File metadata and controls
132 lines (109 loc) · 3.62 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
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
//// Helpers ////////////////////////////////////////////////////////////////
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
function notify(text) {
chrome.notifications.create({
type: "basic",
iconUrl: "/icons/logo64.png",
title: "identifiers.org",
message: text
});
}
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
//// Context menu handlers ////////////////////////////////////////////////////////////////
const handleConvertUrl = async (url) => {
const resolvedData = await fetch ("https://resolver.api.identifiers.org/reverse/byPrefix", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({apiVersion: "1.0", payload: {url}})
})
.then(res => res.status === 200 ? res.json() : null, (reason) => notify(reason))
if (!resolvedData) {
notify(`'${url}' could not be converted`);
} else {
const { possible_idorg_url } = resolvedData.payload;
const tab = await getCurrentTab();
console.log(tab);
await chrome.scripting.executeScript({
target: { tabId: tab.id },
args: [possible_idorg_url],
func: async (url) => {
// This code runs in the content script context where the clipboard is available
await navigator.clipboard.writeText(url);
},
});
notify("URI copied to clipboard");
}
}
const handleResolveCurie = async (curie) => {
const resolvedData = await fetch ("https://resolver.api.identifiers.org/" + curie)
.then(res => res.ok ? res.json() : null, (reason) => notify(reason))
if (!resolvedData) {
notify(`Failed tor resolve '${curie}'`);
} else {
const { rawRequest } = resolvedData.payload.parsedCompactIdentifier;
chrome.tabs.create({url:`http://identifiers.org/${rawRequest}`});
}
}
async function handleResolveCurieClick(info) {
const curie = info.linkText || info.selectionText;
await handleResolveCurie(curie);
}
async function handleConvertUrlClicked(info) {
const url = info.linkUrl || info.selectionText;
if (isValidHttpUrl(url)) {
await handleConvertUrl(url);
} else {
notify(`Invalid URL: '${url}'`);
}
}
async function handleConvertThisPageClicked(info) {
const { pageUrl } = info
await handleConvertUrl(pageUrl);
}
//// Context menu setup ////////////////////////////////////////////////////////////////
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "idorg-resolve-curie",
title: "Resolve selected CURIE",
contexts: ["selection", "link"],
}, () => chrome.runtime.lastError
);
chrome.contextMenus.create({
id: "idorg-convert-url",
title: "Get identifiers.org URI for selected URL",
contexts: ["selection", "link"],
}, () => chrome.runtime.lastError
);
chrome.contextMenus.create({
id: "idorg-convert-this-page",
title: "Get identifiers.org URI for this page",
contexts: ["all"],
}, () => chrome.runtime.lastError
);
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "idorg-resolve-curie") {
await handleResolveCurieClick(info);
}
if (info.menuItemId === "idorg-convert-url") {
await handleConvertUrlClicked(info);
}
if (info.menuItemId === "idorg-convert-this-page") {
await handleConvertThisPageClicked(info)
}
});