-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poblink.user.js
240 lines (223 loc) · 6.88 KB
/
Poblink.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// ==UserScript==
// @name Poblink
// @version 0.5.0
// @description A script to add path of building links next to build share links
// @license MIT
// @author Dullson
// @namespace https://github.com/Dullson
// @include https://*
// @grant none
// @run-at document-idle
// @updateURL https://github.com/Dullson/Poblink/raw/master/Poblink.user.js
// @downloadURL https://github.com/Dullson/Poblink/raw/master/Poblink.user.js
// @supportURL https://github.com/Dullson/Poblink/issues
// ==/UserScript==
const sitePresets = [
{
name: 'PoeForum',
regex: /^https:\/\/www\.pathofexile\.com\/forum\/view-thread\//,
run: poeForum,
},
{
name: 'Youtube',
regex: /^https:\/\/www\.youtube\.com\//,
run: youtube,
},
{
name: 'GoogleDocs',
regex: /^https:\/\/docs\.google\.com\//,
run: googleDocs,
},
{
name: 'Reddit',
regex: /^https:\/\/\w{3}\.reddit\.com\//,
run: reddit,
},
{
name: 'Pastebin',
regex: /^https:\/\/pastebin\.com\/\w{8}/,
run: pastebin,
},
];
const linkSelectors = [
{
id: 'Pastebin',
url: 'pastebin.com',
regex: /pastebin\.com\/(\w{8})/,
query: 'a[href^="https://pastebin.com/"]',
tryParse: function (str) {
let match = str.match(this.regex);
if (match) return `pob:${this.id.toLowerCase()}/${match[1]}`;
},
},
{
id: 'PoBBin',
url: 'pobb.in',
regex: /pobb\.in\/([\w-]{12})/,
query: 'a[href^="https://pobb.in/"]',
tryParse: function (str) {
let match = str.match(this.regex);
if (match) return `pob:${this.id.toLowerCase()}/${match[1]}`;
},
},
{
id: 'PoeNinja',
url: 'poe.ninja',
regex: /poe\.ninja\/pob\/(\w+)/,
query: 'a[href^="https://poe.ninja/pob/"]',
tryParse: function (str) {
let match = str.match(this.regex);
if (match) return `pob:${this.id.toLowerCase()}/${match[1]}`;
},
},
];
(function initialize() {
addPoblinkStyle("margin: 1ex; white-space: nowrap;");
for (const preset of sitePresets) {
if (document.location.href.match(preset.regex)) {
console.log(`Poblink: Running ${preset.name} preset.`);
preset.run();
return;
}
}
console.log("Poblink: Running Generic preset.");
genericPreset();
})();
function poeForum() {
for (const selector of linkSelectors) {
let snapshot = document.evaluate(
`//text()[contains(., '${selector.url}') and not(ancestor::a)]`
, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < snapshot.snapshotLength; i++) {
let element = snapshot.snapshotItem(i);
let pobUrl = selector.tryParse(element.textContent);
if (!pobUrl) continue;
let poblinkElement = createPoblinkElement(pobUrl);
element.parentElement.insertBefore(poblinkElement, element.nextSibling);
}
}
staticSearch(linkSelectors);
}
function pastebin() {
const selector = linkSelectors.find(s => s.id == "Pastebin");
let pobUrl = selector.tryParse(document.location.href);
if (!pobUrl) return;
let poblinkElement = createPoblinkElement(pobUrl);
addPoblinkStyle("margin: 0 0 0 20px");
let infoPanel = document.querySelector(".info-bottom");
infoPanel.appendChild(poblinkElement);
}
function youtube() {
const selectors = linkSelectors.map((s) => Object.assign({}, s, {
regex: new RegExp(s.regex.source.replaceAll('\\/', '%2F'), s.regex.flags),
query: s.query.replace('^="https://', '*="').replaceAll('/', '%2F'),
}));
startObserver(selectors);
}
function googleDocs() {
const selectors = linkSelectors.map((s) => Object.assign({}, s, {
query: s.query.replace('^="https://', '*="')
}));
staticSearch(selectors);
startObserver(selectors);
}
function reddit() {
if (!document.location.host.startsWith('old')) { // new design
addPoblinkStyle(`
color: var(--newCommunityTheme-linkText);
text-decoration: underline;
`);
}
genericPreset();
}
function genericPreset() {
staticSearch(linkSelectors);
startObserver(linkSelectors);
}
function staticSearch(selectors) {
for (const selector of selectors) {
for (const element of document.querySelectorAll(selector.query)) {
let pobUrl = selector.tryParse(element.href);
if (!pobUrl) continue;
console.log(`Poblink: generating link for ${element.href}`);
let poblinkElement = createPoblinkElement(pobUrl);
element.parentElement.insertBefore(poblinkElement, element.nextSibling);
}
}
}
function startObserver(selectors) {
new MutationObserver((mutationRecords, observer) => {
for (const mutation of mutationRecords) {
for (const selector of selectors) {
let elements = [];
for (const addedNode of mutation.addedNodes) {
if (!addedNode.tagName) continue;
if (addedNode.matches(selector.query)) {
elements.push(addedNode);
} else {
for (const e of addedNode.querySelectorAll(selector.query)) {
elements.push(e);
}
}
}
if (mutation.target.matches(selector.query)) {
elements.push(mutation.target);
}
for (const element of elements) {
let pobUrl = selector.tryParse(element.href);
if (!pobUrl) continue;
const nextNode = element.nextSibling;
if (nextNode && nextNode.matches && nextNode.matches('.poblink')) {
if (nextNode.href !== pobUrl) {
nextNode.href = pobUrl;
}
}
else {
console.log(`Poblink: generating link for ${element.href}`);
let poblinkElement = createPoblinkElement(pobUrl);
element.parentElement.insertBefore(poblinkElement, nextNode);
}
}
}
for (const removedNode of mutation.removedNodes) {
if (!removedNode.tagName) continue;
if (removedNode.matches('.poblink')) {
mutation.target.insertBefore(removedNode, mutation.nextSibling);
}
}
}
})
.observe(document.documentElement, {
childList: true,
subtree: true
});
}
function createElement(str) {
let temp = document.createElement('template');
temp.innerHTML = str.trim();
return temp.content.firstChild;
}
function createPoblinkElement(url) {
return createElement(`<a href="${url}" class="poblink">Poblink</a>`);
}
function addPoblinkStyle(style) {
const styleNode = document.createElement('style');
styleNode.innerHTML = `.poblink {${style}}`
document.head.appendChild(styleNode);
}
function elementReady(selector) {
return new Promise((resolve, reject) => {
let el = document.querySelector(selector);
if (el) { resolve(el); }
new MutationObserver((mutationRecords, observer) => {
Array.from(document.querySelectorAll(selector)).forEach((element) => {
resolve(element);
observer.disconnect();
});
})
.observe(document.documentElement, {
childList: true,
subtree: true,
});
});
}