-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcreatelink.js
More file actions
168 lines (147 loc) · 4.48 KB
/
Copy pathcreatelink.js
File metadata and controls
168 lines (147 loc) · 4.48 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
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
const utils = require('./utils')
class CreateLink {
get formats() {
return this.readFormats();
}
applyFilter(tabId, def, data) {
if (def.filter) {
var m = def.filter.match(/^s\/(.+?)\/(.*?)\/(\w*)$/);
if (m) {
var r = new RegExp(m[1], m[3]);
data = data.replace(r, m[2]);
} else {
return utils.sendMessageToTab(tabId, {type: 'evaluateFilter', code: def.filter, string: data})
}
}
return Promise.resolve(data);
}
formatLinkText(def, url, text, textonly, title, inputs) {
text = text || ''
textonly = textonly || ''
var data = def.format.
replace(/%url%/g, url).
replace(/%text%/g, text.replace(/\n/g, ' ')).
replace(/%textonly%/g, textonly.replace(/\n/g, ' ')).
// TODO: allow customizing instead of hardcoding, eg using regex replace
replace(/%onnonemptyselection%/g, textonly == "" ? "" : " TITLE: ").
replace(/%text_n%/g, text).
replace(/%text_br%/g, text.replace(/\n/g, '<br />\n')).
replace(/%text_md%/g, text.replace(/[|\\`*_{}\[\]()#+\-.!]/g, '\\$&')).
replace(/%title%/g, title).
replace(/%newline%/g, '\n').
replace(/%htmlEscapedText%/g, escapeHTML(text)).
replace(/\\t/g, '\t').
replace(/\\n/g, '\n').
replace(/\\r/g, '\r');
let index = 0
return data.replace(/%input%/g, (s) => {
return inputs[index++]
})
}
getInputs(def, tabId) {
const m = def.format.match(/%input%/g)
if (m) {
return Promise.all( m.map( () => {
return utils.showInputDialog(tabId)
}) )
} else {
return Promise.resolve([])
}
}
formatInTab(formatId, info, tab) {
var url;
if (info.mediaType === 'image') {
url = info.srcUrl;
} else {
url = info.linkUrl || info.pageUrl || tab.url;
}
var text = info.selectionText || tab.title;
var textonly = info.selectionText;
var title = tab.title;
var def = this.formats[formatId]
return this.formatString(tab.id, def, url, text, textonly, title)
}
formatString(tabId, def, url, text, textonly, title) {
return this.getInputs(def, tabId).then( (inputs) => {
const linkText = this.formatLinkText(def, url, text, textonly, title, inputs)
return this.applyFilter(tabId, def, linkText)
})
}
copyToClipboard(text) {
const backgroundPage = chrome.extension.getBackgroundPage()
let textarea = document.getElementById('clipboard_object');
if (!textarea) {
textarea = backgroundPage.document.createElement('textarea')
textarea.setAttribute('id', 'clipboard_object')
backgroundPage.document.body.appendChild(textarea)
}
textarea.value = text;
textarea.select();
document.execCommand("copy");
}
setDefaultFormat(value) {
localStorage[defaultFormatKey] = value;
};
getDefaultFormat() {
return localStorage[defaultFormatKey];
};
getDefaultFormatId() {
const formatName = this.getDefaultFormat()
return this.indexOfFormatByLabel(formatName);
}
setFormatPreferences(formatsString) {
localStorage[formatPreferencesKey] = formatsString;
};
getFormatPreferences() {
return JSON.parse(localStorage[formatPreferencesKey] || '[]');
};
readFormats() {
var formats;
try {
formats = this.getFormatPreferences();
} catch(e) {
}
if ( !formats || formats.length == 0 ) {
formats = CreateLink.default_formats;
}
return formats;
};
indexOfFormatByLabel(label) {
var formats = this.formats;
for (var i = 0, len = formats.length; i < len; i++) {
var item = formats[i];
if (item.label === label) {
return i;
}
}
return -1;
};
static defaultFormats() {
return CreateLink.default_formats
}
}
var formatPreferencesKey = 'format_preferences';
var defaultFormatKey = 'defaultFormat';
function escapeHTML(text) {
return text ? text.replace(/[&<>'"]/g, convertHTMLChar) : text;
}
function convertHTMLChar(c) { return charMap[c]; }
var charMap = {
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
};
function showPrompt(text, pos, subject) {
var msg = "Please enter the input text for \n" + subject;
var s = window.prompt(msg);
return (s === null) ? "" : s;
}
CreateLink.default_formats = [
{label: "Plain text", format: '%text% %url%' },
{label: "HTML", format: '<a href="%url%">%htmlEscapedText%</a>' },
{label: "markdown", format: '[%text_md%](%url%)' },
{label: "mediaWiki", format: '[%url% %text%]' },
];
module.exports = CreateLink