-
Notifications
You must be signed in to change notification settings - Fork 1
/
url_clean_redir.user.js
89 lines (78 loc) · 2.82 KB
/
url_clean_redir.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
// ==UserScript==
// @name URL Clean with redirect
// @description Clean / minimize large URLs by stripping tracking info. This version redirect to remove sub domains
// @license BSD 3-Clause
// @author Duckle29
// @namespace https://github.com/Duckle29
// @run-at document-start
// @icon https://avatars3.githubusercontent.com/u/2756925?v=3&s=200
// @homepageURL https://github.com/Duckle29/url_clean
// @downloadURL https://github.com/Duckle29/url_clean/raw/main/url_clean_redir.user.js
// @updateURL https://github.com/Duckle29/url_clean/raw/main/url_clean_redir.user.js
// @version 1.1.6
//
// @include /^https?:\/\/(?:www\.)?([a-zA-Z]{2,3}\.)?rs-online\.com\/web\/p\/.*/
// @include /^https?:\/\/(?:www\.)?([a-zA-Z]{1,3}\.)?aliexpress\.(?:(?:co.)?[a-zA-Z]{2,3})\/(item|store\/product)\/.*/
// @include /^https?:\/\/(?:www\.)?ebay\.(?:co.)?[a-zA-Z]{2,3}\/itm.*/
// @include /^https?:\/\/(?:www\.)?amazon\.(?:co.)?[a-zA-Z]{2,3}\/.*/
// @history 1.1.6 Updated regex for aliexpress to work with different TLDs
// @history 1.1.5 Added redirect version of script
// @history 1.1.4 Added RS online
// @history 1.1.3 Fixed regex replace for ebay
// @history 1.1.2 Fixed regex to match URL encoding
// @history 1.1.1 Fixed regex for Amazon
// @history 1.1 Added Amazon
// @history 1.0 Initial release
// ==/UserScript==
(function()
{
'use strict';
let sites =
[
[/^(https?:\/\/(?:www\.)?(?:[a-zA-Z]{2,3}\.)?rs-online\.com\/web\/p\/[^\/]*\/)(\d*)/, [1, 2]],
[/^(https?:\/\/(?:www\.)?ebay\.(?:(?:co.)?[a-zA-Z]{2,3})\/itm)(?:\/[0-9a-zA-Z%\-]+)*(\/\d+)/, [1, 2]],
[/^(https?:\/\/)(?:[a-zA-Z]{1,3}\.)?(aliexpress\.(?:(?:co.)?[a-zA-Z]{2,3})\/(?:item|store\/product))(\/[0-9_]+[.]html(?=$|[?]))/, [1, 2, 3]],
[/^(https?:\/\/(?:www\.)?amazon\.(?:co.)?[a-zA-Z%]{2,3}\/.*\/dp\/)(.*)(?:\\)?\?/, [1, 2]]
];
for (let i=0; i<sites.length; i++)
{
regReplace(sites[i][0], sites[i][1])
}
function regReplace(expression, combination)
{
let groups = window.location.href.match(expression)
if (groups == null)
{
return
}
if (groups.length < Math.max(...combination))
{
return
}
let cleaned_url = ""
for (let k=0; k<combination.length; k++)
{
cleaned_url += groups[combination[k]]
}
let browser_url = window.location.href
// If the site has inserted 'www.' don't infinitely loop trying to remove it.
if (browser_url.split('.')[0].includes('www') && Math.abs(browser_url.length - cleaned_url.length) === 4)
{
return
}
if (cleaned_url != browser_url)
{
try
{
history.replaceState(null, '', cleaned_url);
}
catch(err)
{
if (err instanceof DOMException)
{
window.location.replace(cleaned_url);
}
}
}
}
})();