-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrueTweetLinks.user.js
48 lines (45 loc) · 1.7 KB
/
TrueTweetLinks.user.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
// ==UserScript==
// @author An_dz
// @version 1.4
// @name TrueTweetLink
// @description Removes the Twitter tracking in tweet links
// @date 2018 February 22
// @include https://*twitter.com/*
// @include http://*twitter.com/*
// @run-at document-start
// @grant none
// @license MIT
// @downloadURL https://github.com/An-dz/TrueLinks/raw/master/TrueTweetLinks.user.js
// ==/UserScript==
(function TrueTweetLink(){
function makeTrueLinks(element) {
var links = element.querySelectorAll("a[data-expanded-url], a[data-expanded-path], a[href*='://t.co'][title^='http']");
// change links
links.forEach( function (link) {
// Twitter adds a data- attribute, if it exists we do the changes
if(link.hasAttribute("data-expanded-url")) {
link.href = link.getAttribute("data-expanded-url");
}
// If using mobile.twitter pic.twitter has a different way
else if (link.hasAttribute("data-expanded-path")) {
link.href = link.getAttribute("data-url");
}
// if the above attributes do not exist sometimes we can get from the title
else if (link.hasAttribute("title")) {
link.href = link.getAttribute("title");
}
});
}
// mutation observer is asynchronous, the link will load unchanged for a fraction of miliseconds but this should make the page more responsive
var observer = new MutationObserver(function (changes) {
changes.forEach(function (chg) {
chg.addedNodes.forEach(function (element) {
if (element.parentElement !== null) {
makeTrueLinks(element.parentElement);
}
});
});
});
// starts the mutation observer
observer.observe(document, {childList: true, subtree: true});
}());