forked from apiraino/link_cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.js
131 lines (110 loc) · 3.96 KB
/
copy.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
"use strict";
/*
copy text to clipboard
ref: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types
*/
let global_link_text = '';
function glob_to_regexp (pattern) {
// https://stackoverflow.com/a/24558913
return new RegExp(pattern
.replace(/([.?+^$[\]\\(){}|\/-])/g, "\\$1")
.replace(/\*/g, '.*'));
}
// remove tracking query params
function maybe_clean (orig_url, urls, f) {
let res = '';
for (let idx = 0; idx < urls.length; idx++) {
const patt = glob_to_regexp(urls[idx]);
if (patt.test(orig_url)) {
res = link_cleaner(orig_url, f)['redirectUrl'];
break;
}
}
return (res != '' ? res : orig_url);
}
// rewrite URL
function maybe_clean_url (orig_url, urls, f) {
let res = '';
for (let idx = 0; idx < urls.length; idx++) {
const patt = glob_to_regexp(urls[idx]);
if (patt.test(orig_url)) {
res = f(orig_url)['redirectUrl'];
break;
}
}
return (res != '' ? res : orig_url);
}
// pick one query param with a URL and redirect there
function _redirect_to_query_param (orig_url) {
let res = '';
for (let i = 0; i < urls_to_param_mappers.length; i++) {
const item = urls_to_param_mappers[i];
const query_param = item.query_param ? item.query_param : 'url';
for (let idx = 0; idx < item.urls.length; idx++) {
const patt = glob_to_regexp(item.urls[idx]);
if (patt.test(orig_url)) {
res = redirect_to_query_param(query_param, orig_url)['redirectUrl'];
if (res != '') {
break;
}
}
}
}
return (res != '' ? res : orig_url);
}
// this is only invoked on right-click menu action
function cleaner_entrypoint (orig_link) {
// first check: find a query param with the real URL and redirect there
let new_link = _redirect_to_query_param(orig_link);
// 2nd check: remove tracking query params
// TODO: profile && improve perfs here
new_link = maybe_clean(new_link, all_urls, f_match_utm);
new_link = maybe_clean(new_link, all_urls, f_match_fbclid);
new_link = maybe_clean(new_link, aliexpress_regexp, f_match_all);
new_link = maybe_clean(new_link, fbcontent_regexp, f_match_fbcontent);
new_link = maybe_clean(new_link, instagram_regexp, f_match_igshid);
// 3rd check: (optional) clean Amazon URL
if (settings['clean_amp_links'] === true) {
new_link = maybe_clean_url(new_link, ["amazon"], clean_amazon);
}
// 4th check: (optional) redirect reddit URL
if (settings['redirect_reddit_nojs'] === true) {
new_link = new_link.replace('www', 'old');
}
// console.debug("[copytoclipboard] link cleaned: " + new_link);
return new_link;
}
function copyToClipboard(obj) {
// does not work inside frame
if ((document.activeElement instanceof HTMLIFrameElement) ||
(document.activeElement instanceof HTMLFrameElement)) {
// need to call it twice :/
document.activeElement.blur();
document.activeElement.blur();
}
// if there is a link, clean it, otherwise copy the text
if (obj.linkUrl !== undefined) {
// sequential execution of all cleaners
global_link_text = cleaner_entrypoint(obj.linkUrl);
} else {
global_link_text = obj.selectionText;
}
// trigger cleaned link copy back in clipboard
document.execCommand("copy");
}
/*
Get cleaned link text and copy it back into the clipboard
We need an event for this
*/
function oncopy(event) {
// console.debug("[oncopy] " + global_link_text);
if (global_link_text === "") {
return;
}
// Hide the event from the page to prevent tampering.
event.stopImmediatePropagation();
event.preventDefault();
event.clipboardData.setData("text/plain", global_link_text);
global_link_text = '';
}
document.addEventListener("copy", oncopy, true);