-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonLinkRewrite.user.js
More file actions
59 lines (54 loc) · 2.79 KB
/
AmazonLinkRewrite.user.js
File metadata and controls
59 lines (54 loc) · 2.79 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
// ==UserScript==
// @name Amazon Link Rewriter
// @homepageURL https://github.com/AlgoClaw/MonkeyScripts/blob/main/AmazonLinkRewrite.user.js
// @downloadURL https://raw.githubusercontent.com/AlgoClaw/MonkeyScripts/main/AmazonLinkRewrite.user.js
// @updateURL https://raw.githubusercontent.com/AlgoClaw/MonkeyScripts/main/AmazonLinkRewrite.user.js
// @description null
// @version 2025.07.04
// @grant none
// @include *
// ==/UserScript==
//////////////////////////////////////////////////////////////////////////////////////////////////
// Define REGEX patterns to find and replace
const TSTSTR = 'amazon'
var FIND001 = 'https:\/\/.*amazon\.com.*\/dp(\/[A-z0-9]{10}).*'
var FIND002 = 'https:\/\/.*amazon\.com.*\/dp\/product(\/[A-z0-9]{10}).*'
var FIND003 = 'https:\/\/.*amazon\.com.*\/gp\/product(\/[A-z0-9]{10}).*'
var REPLACE = 'https:\/\/amazon.com\/dp$1'
//////////////////////////////////////////////////////////////////////////////////////////////////
//Run function every 3 seconds
//setTimeout(RewriteLinks, 3000)
setInterval(RewriteLinks, 3000);
// Function to rewrite static links
function RewriteLinks() {
var links;
links = document.evaluate("//a[@href]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
for (var i=0;i<links.snapshotLength;i++) {
var thisLink = links.snapshotItem(i);
if (thisLink !== undefined) { // only rewrite if not undefined
if (thisLink.href.includes(TSTSTR) === true) { // only rewrite if TSTSTR is in link
//console.log(thisLink.href); // link before change
thisLink.href = thisLink.href.replace(RegExp(FIND001),REPLACE);
thisLink.href = thisLink.href.replace(RegExp(FIND002),REPLACE);
thisLink.href = thisLink.href.replace(RegExp(FIND003),REPLACE);
//console.log(thisLink.href); // link after change
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Rewrite link on mouse click (left, middle, and right)
document.addEventListener("click", (e) => change_link(e), false) // left click
document.addEventListener("auxclick", (e) => change_link(e), false) // right/middle click
function change_link(event) {
const thisLink = event.currentTarget.activeElement;
if (thisLink.href !== undefined) { // only rewrite if not undefined
if (thisLink.href.includes(TSTSTR) === true) { // only rewrite if TSTSTR is in link
//console.log(link.href); // link before change
thisLink.href = thisLink.href.replace(RegExp(FIND001),REPLACE);
thisLink.href = thisLink.href.replace(RegExp(FIND002),REPLACE);
thisLink.href = thisLink.href.replace(RegExp(FIND003),REPLACE);
//console.log(link.href); // link after change
}
}
}