forked from hnakamur/FormatLink-Firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard-helper.js
136 lines (125 loc) · 3.6 KB
/
clipboard-helper.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
// This function must be called in a visible page, such as a browserAction popup
// or a content script. Calling it in a background page has no effect!
function FormatLink_copyTextToClipboard(text) {
function oncopy(event) {
document.removeEventListener("copy", oncopy, true);
// Hide the event from the page to prevent tampering.
event.stopImmediatePropagation();
// Overwrite the clipboard content.
event.preventDefault();
event.clipboardData.setData("text/plain", text);
}
document.addEventListener("copy", oncopy, true);
// Requires the clipboardWrite permission, or a user gesture:
document.execCommand("copy");
}
function FormatLink_formatLinkAsText(format, newline, linkUrl, linkText) {
function getFirstLinkInSelection(selection) {
return selection.anchorNode.parentNode.href;
}
function formatURL(format, url, title, selectedText, newline) {
let text = '';
let work;
let i = 0, len = format.length;
function parseLiteral(str) {
if (format.substr(i, str.length) === str) {
i += str.length;
return str;
} else {
return null;
}
}
function parseString() {
let str = '';
if (parseLiteral('"')) {
while (i < len) {
if (parseLiteral('\\')) {
if (i < len) {
str += format.substr(i++, 1);
} else {
throw new Error('parse error expected "');
}
} else if (parseLiteral('"')) {
return str;
} else {
if (i < len) {
str += format.substr(i++, 1);
} else {
throw new Error('parse error expected "');
}
}
}
} else {
return null;
}
}
function processVar(value) {
let work = value;
while (i < len) {
if (parseLiteral('.s(')) {
let arg1 = parseString();
if (arg1 && parseLiteral(',')) {
let arg2 = parseString();
if (arg2 && parseLiteral(')')) {
let regex = new RegExp(arg1, 'g');
work = work.replace(regex, arg2);
} else {
throw new Error('parse error');
}
} else {
throw new Error('parse error');
}
} else if (parseLiteral('}}')) {
text += work;
return;
} else {
throw new Error('parse error');
}
}
}
while (i < len) {
if (parseLiteral('\\')) {
if (parseLiteral('n')) {
text += newline;
// isWindows ? "\r\n" : "\n";
} else if (parseLiteral('t')) {
text += "\t";
} else {
text += format.substr(i++, 1);
}
} else if (parseLiteral('{{')) {
if (parseLiteral('title')) {
processVar(title);
} else if (parseLiteral('url')) {
processVar(url);
} else if (parseLiteral('text')) {
processVar(selectedText ? selectedText : title);
}
} else {
text += format.substr(i++, 1);
}
}
return text;
}
let title = document.title;
let text = linkText;
let href = linkUrl;
let selection = window.getSelection();
if (selection.rangeCount > 0) {
let selectionText = selection.toString().trim();
if (!text && selectionText) {
text = selectionText;
}
let hrefInSelection = getFirstLinkInSelection(selection);
if (!href && hrefInSelection) {
href = hrefInSelection;
}
}
if (!text) {
text = title;
}
if (!href) {
href = window.location.href;
}
return formatURL(format, href, title, text, newline);
}