/
retrieve-url.js
176 lines (156 loc) · 4.52 KB
/
retrieve-url.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
import {
log as internalLogger,
} from './common.js';
import * as Permissions from './permissions.js';
function log(...args) {
internalLogger('common/retrieve-url', ...args);
}
export const kTYPE_X_MOZ_URL = 'text/x-moz-url';
export const kTYPE_URI_LIST = 'text/uri-list';
export const kTYPE_MOZ_TEXT_INTERNAL = 'text/x-moz-text-internal';
const kBOOKMARK_FOLDER = 'x-moz-place:';
const ACCEPTABLE_DATA_TYPES = [
kTYPE_URI_LIST,
kTYPE_X_MOZ_URL,
kTYPE_MOZ_TEXT_INTERNAL,
'text/plain'
];
let mFileURLResolver = null;
export async function registerFileURLResolver(resolver) {
mFileURLResolver = resolver;
}
export async function fromDragEvent(event) {
log('fromDragEvent ', event);
const dt = event.dataTransfer;
const urls = [];
if (dt.files.length > 0) {
for (const file of dt.files) {
if (typeof mFileURLResolver == 'function') {
urls.push(await mFileURLResolver(file));
}
else {
// Created object URLs need to be revoked by tryRevokeObjectURL()
// in common/common.js, after they are loaded.
urls.push(URL.createObjectURL(file));
}
}
}
else {
for (const type of ACCEPTABLE_DATA_TYPES) {
const urlData = dt.getData(type);
if (urlData)
urls.push(...fromData(urlData, type));
if (urls.length)
break;
}
for (const type of dt.types) {
if (!/^application\/x-treestyletab-drag-data;(.+)$/.test(type))
continue;
const params = RegExp.$1;
const providerId = /provider=([^;&]+)/.test(params) && RegExp.$1;
const dataId = /id=([^;&]+)/.test(params) && RegExp.$1;
try {
const dragData = await browser.runtime.sendMessage(providerId, {
type: 'get-drag-data',
id: dataId
});
if (!dragData || typeof dragData != 'object')
break;
for (const type of ACCEPTABLE_DATA_TYPES) {
const urlData = dragData[type];
if (urlData)
urls.push(...fromData(urlData, type));
if (urls.length)
break;
}
}
catch(_error) {
}
}
}
log(' => retrieved: ', urls);
return sanitizeURLs(urls);
}
export async function fromClipboard() {
const urls = [];
if (!(await Permissions.isGranted(Permissions.CLIPBOARD_READ)) ||
typeof navigator.clipboard.read != 'function')
return urls;
const clipboardContents = await navigator.clipboard.read();
log('fromClipboard ', clipboardContents);
for (const item of clipboardContents) {
for (const type of item.types) {
const maybeUrlBlob = await item.getType(type);
const maybeUrlString = await maybeUrlBlob.text();
if (maybeUrlString)
urls.push(...fromData(maybeUrlString, type));
if (urls.length)
break;
}
}
log(' => retrieved: ', urls);
return sanitizeURLs(urls);
}
function sanitizeURLs(urls) {
const filteredUrls = urls.filter(url =>
url &&
url.length &&
url.indexOf(kBOOKMARK_FOLDER) == 0 ||
!/^\s*(javascript|data):/.test(url)
);
log('sanitizeURLs filtered: ', filteredUrls);
const fixedUrls = filteredUrls.map(fixupURIFromText);
log('sanitizeURLs fixed: ', fixedUrls);
return fixedUrls;
}
function fromData(data, type) {
log('fromData: ', type, data);
switch (type) {
case kTYPE_URI_LIST:
return data
.replace(/\r/g, '\n')
.replace(/\n\n+/g, '\n')
.split('\n')
.filter(line => {
return line.charAt(0) != '#';
});
case kTYPE_X_MOZ_URL:
return data
.trim()
.replace(/\r/g, '\n')
.replace(/\n\n+/g, '\n')
.split('\n')
.filter((_line, index) => {
return index % 2 == 0;
});
case kTYPE_MOZ_TEXT_INTERNAL:
return data
.replace(/\r/g, '\n')
.replace(/\n\n+/g, '\n')
.trim()
.split('\n');
case 'text/plain':
return data
.replace(/\r/g, '\n')
.replace(/\n\n+/g, '\n')
.trim()
.split('\n')
.map(line => {
return /^\w+:\/\/.+/.test(line) ? line : `ext+treestyletab:search:${line}`;
});
}
return [];
}
function fixupURIFromText(maybeURI) {
if (/^(ext\+)?\w+:/.test(maybeURI))
return maybeURI;
if (/^([^\.\s]+\.)+[^\.\s]{2}/.test(maybeURI))
return `http://${maybeURI}`;
return maybeURI;
}