-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacebook-clean-links.user.js
More file actions
146 lines (123 loc) · 3.48 KB
/
Copy pathfacebook-clean-links.user.js
File metadata and controls
146 lines (123 loc) · 3.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
// ==UserScript==
// @name Facebook 乾淨連結
// @description 清除臉書為外部連結附加的轉址等處理
// @version 1.0.1
// @license MIT
// @author bootleq
// @namespace bootleq.com
// @homepageURL https://github.com/bootleq/user-scripts
//
// @match https://www.facebook.com/*
// @run-at document-idle
// @grant none
// @noframes
// ==/UserScript==
// Target behaviors:
//
// - Link href transformed on click
// <a href='foo'> changes its `href` to 'l.facebook.com/l.php?u=foo' after user click it.
//
// - Link href had `?fbclid=...` params appended
// Search params to be removed
// ref: https://github.com/mpchadwick/tracking-query-params-registry/blob/master/_data/params.csv
const unwantedParams = [
'fbclid',
// 'utm_content',
// 'utm_term',
// 'utm_campaign',
// 'utm_medium',
// 'utm_source',
// 'utm_id',
];
// Known FB domains, to determine if a link is external
const fbDomains = ['www.facebook.com']
const linkSelector = 'a[role="link"][href^="http"]';
const unwantedEvents = [
'contextmenu',
'click',
];
function isExternalOrTracking(href) {
const domain = (new URL(href)).hostname;
return !fbDomains.includes(domain);
}
function cleanupURL(url) {
const u = new URL(url);
const search = u.search;
// Real link is in the ?u=foo part
if (/^lm?.facebook.com$/i.test(u.hostname) && u.pathname === '/l.php' && u.search.startsWith('?u=')) {
const newHref = decodeURIComponent(u.href.match(/u=([^&#$]+)/i)[1]);
if (newHref.startsWith('https://')) {
return cleanupURL(newHref);
}
}
for (const paramName of unwantedParams) {
u.searchParams.delete(paramName)
}
return u.href;
}
function stopPropagation(event) {
const el = event.target;
const link = el.closest(linkSelector);
const btn = el.closest('[role="button"]')
if (btn) {
// console.log('👌 forgive button', btn, el);
return;
}
if (link && isExternalOrTracking(link.href)) {
event.stopPropagation();
event.stopImmediatePropagation();
}
}
function unbindEvents(el) {
for (const eventName of unwantedEvents) {
el.addEventListener(eventName, stopPropagation, true);
}
}
function mutateLink(node) {
const href = node.href;
if (isExternalOrTracking(href)) {
const cleared = cleanupURL(href);
if (cleared !== href) {
node.href = cleared;
}
}
}
function removeEventHandlers() {
const container = document.querySelector('body > div[id^="mount"]');
if (container) {
unbindEvents(container);
} else {
console.log('Container not found.');
}
}
function init() {
removeEventHandlers();
document.querySelectorAll(linkSelector).forEach(mutateLink);
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
const link = node.matches(linkSelector) ? node : node.querySelector(linkSelector);
if (link) {
mutateLink(link);
}
}
});
} else if (mutation.type === 'attributes' && mutation.target.matches(linkSelector)) {
const link = mutation.target;
mutateLink(link);
}
}
});
observer.observe(
document.body,
{
attributes: true,
attributeFilter: ['href'],
childList: true,
subtree: true
}
);
}
init();