-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_link.js
64 lines (60 loc) · 2.41 KB
/
generate_link.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
function generateLink(tab) {
function createRedirectHTML(myURL, myTitle) {
var doc = document.implementation.createHTMLDocument(myTitle);
var newMeta = document.createElement('meta');
newMeta.httpEquiv = 'refresh';
newMeta.setAttribute('charset', 'utf-8');
newMeta.content = '0; url=' + myURL;
doc.head.appendChild(newMeta);
var newPar = document.createElement('p');
var before = document.createTextNode('Loading ');
var after = document.createTextNode('...');
var newAnchor = document.createElement('a');
newAnchor.href = myURL;
newAnchor.text = myTitle;
newPar.appendChild(before);
newPar.appendChild(newAnchor);
newPar.appendChild(after);
doc.body.appendChild(newPar);
return doc;
}
function parseURL(URL) {
var newAnchor = document.createElement('a');
newAnchor.href = tab.url;
return newAnchor;
}
function sanitizeFilename(filename) {
return filename.replace(/</g, '_')
.replace(/>/g, '_')
.replace(/"/g, '_')
.replace(/'/g, '_')
.replace(/[\\/]+/g, '_')
.replace(/[\u200e\u200f\u202a-\u202e]/g, '_')
.replace(/[\x00-\x1f\x7f-\x9f:*?|"<>;,+=\[\]]+/g, '_')
.replace(/^[\s\u180e.]+|[\s\u180e.]+$/g, '_')
.replace(/[\uFE00-\uFE0F]/g, '') // variation selectors
.replace(/\u200D/g, '') // zero-width joiner
.replace(/\u00A0/g, ' '); // no -break space
}
// https://searchfox.org/mozilla-central/source/toolkit/components/downloads/DownloadPaths.jsm#68
function getFilename(tab) {
var hostname = parseURL(tab.url).hostname;
var filename = 'link-to-' + tab.title + '_' + hostname + '.link.html';
filename = filename.split(' ').join('_');
filename = sanitizeFilename(filename);
return filename;
}
var payload = createRedirectHTML(tab.url, tab.title);
var serializer = new XMLSerializer();
var HTMLString = serializer.serializeToString(payload);
var payloadBlob = new Blob([HTMLString], {type: 'text/html'});
var payloadURL = URL.createObjectURL(payloadBlob);
var payload_filename = getFilename(tab);
var downloading = browser.downloads.download({
url: payloadURL,
filename: payload_filename,
conflictAction: 'uniquify',
saveAs: true,
});
}
browser.browserAction.onClicked.addListener(generateLink);