-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (107 loc) · 4.09 KB
/
script.js
File metadata and controls
131 lines (107 loc) · 4.09 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
document.getElementById('snap').addEventListener('click', async () => {
const sendStatus = document.getElementById('send-status');
sendStatus.classList.add('hidden');
sendStatus.innerHTML = '';
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const status = document.getElementById('snap-status');
status.innerText = "Capturing...";
// Generate screenshot of the page.
chrome.runtime.sendMessage({
action: "take_screenshot",
tabId: tab.id,
width: 1280,
height: 960
}, (response) => {
if (response?.success) {
status.innerHTML = "<br>";
document.getElementById('result-image').src = response.dataUrl;
document.getElementById('image-data').value = response.dataUrl;
} else {
status.innerText = "Error: " + (response?.error || "Unknown");
}
});
const titlefield = document.getElementById('title');
titlefield.classList.remove('hidden');
titlefield.value = tab.title;
const linkurl = document.getElementById('linkurl');
linkurl.classList.remove('hidden');
linkurl.value = tab.url;
const send = document.getElementById('send');
send.classList.remove('hidden');
});
document.getElementById('send').addEventListener('click', async () => {
const linkurl = document.getElementById('linkurl');
const titlefield = document.getElementById('title');
const imagedata = document.getElementById('image-data');
const sendStatus = document.getElementById('send-status');
sendStatus.innerText = 'Sending!...';
sendStatus.classList.remove('hidden');
const { apiKey, debugMode } = await chrome.storage.sync.get({ apiKey: 'EMPTY', debugMode: false });
// If the debugMode is set then swap the destination to the ddev instance.
if (debugMode) {
var domain = 'hashbangcode.ddev.site';
} else {
var domain = 'www.hashbangcode.com';
}
// Create the endpoint addresses we will use.
const tokenUrl = 'https://' + domain + '/session/token';
const linkCreateUrl = 'https://' + domain + '/api/links/v1/receive-link';
const data = {
link: linkurl.value,
title: titlefield.value,
imagedata: imagedata.value
};
try {
// Fetch the token.
const tokenResponse = await fetch(tokenUrl, {
method: 'GET',
headers: {
'Content-Type': 'text/plain',
}
});
// Extract the token.
const tokenResult = await tokenResponse.text();
// Send link data with our key and csrf token.
const response = await fetch(linkCreateUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Hashbangcode-Extension': 'link-it',
'X-CSRF-Token': tokenResult,
'api-key': apiKey,
},
body: JSON.stringify(data)
});
// Get the response.
const result = await response.json();
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
// Set up the message and display it.
let successMessage = '';
if (result.url) {
successMessage += `Success!<br>`
successMessage = `<a href="${result.url}" target="_blank">${result.message}</a>`;
}
else if (result.message) {
successMessage += `Success!<br>`
successMessage = `${result.message}`;
}
sendStatus.innerHTML = successMessage;
} catch (error) {
// Display error.
let failureMessage = '';
failureMessage += `Failed!<br>`
failureMessage += error.message;
sendStatus.innerHTML = failureMessage;
}
});
document.getElementById('go-to-options').addEventListener('click', () => {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
// Fallback for older versions of Chrome (pre-version 42)
window.open(chrome.runtime.getURL('options.html'));
}
window.close();
});