Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
Latest commit
263 lines (231 loc) · 6.75 KB
/
Copy pathcontent.js
File metadata and controls
263 lines (231 loc) · 6.75 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// content.js — granular asset/menu swapping based on user options
(asyncfunctionmain(){
const{ urlSwapper }=awaitchrome.storage.sync.get("urlSwapper");
if(!urlSwapper)return;
const{
enabled,
source: rawSource="",
dest: rawDest="",
originOnly =false,
switches ={},
menuSelectors =[],
}=urlSwapper;
if(!enabled)return;
letsource=rawSource.trim();
letdest=rawDest.trim();
if(!source||!dest)return;
constnormalize=(u)=>{
try{
consturl=newURL(u);
returnurl.origin;// for originOnly compare
}catch{
returnu.replace(/\/+$/,"");
}
};
constsourceOrigin=normalize(source);
constdestOrigin=normalize(dest);
// Determine if we are on source site
constonSourceSite=(()=>{
if(originOnly){
try{
returnnewURL(location.href).origin===sourceOrigin;
}catch{
returnlocation.href.includes(sourceOrigin);
}
}
// fallback: substring
returnlocation.href.includes(source);
})();
if(!onSourceSite)return;
// Build match & replace functions
constreplaceVal=(val)=>{
if(!val||typeofval!=="string")returnval;
if(originOnly){
// Replace only when URL starts with source origin
if(val.startsWith(sourceOrigin)){
returndestOrigin+val.slice(sourceOrigin.length);
}
returnval;
}
// Non-origin mode: prefer prefix, fallback to global replace
if(val.startsWith(source))returndest+val.slice(source.length);
if(val.includes(source))returnval.split(source).join(dest);
returnval;
};
// Which kinds to swap
const{
swapImages =true,
swapCSS =true,
swapJS =true,
swapMedia =false,
swapIframes =false,
swapInlineCSS =false,
}=switches;
// Attribute allowlists per tag, built from switches
consttagMap=newMap();
if(swapImages){
tagMap.set("IMG",newSet(["src","srcset","style"]));
tagMap.set("SOURCE",newSet(["src","srcset"]));
// lazy attrs are handled globally below
}
if(swapCSS){
// only stylesheets or preload as style/script
tagMap.set("LINK",newSet(["href","rel","as"]));
}
if(swapJS){
tagMap.set("SCRIPT",newSet(["src"]));
}
if(swapMedia){
tagMap.set("VIDEO",newSet(["src","poster","style"]));
tagMap.set("AUDIO",newSet(["src"]));
}
if(swapIframes){
tagMap.set("IFRAME",newSet(["src"]));
}
// Always process STYLE tag text only if swapInlineCSS is on
constprocessStyleTag=swapInlineCSS;
// Common lazy-load attributes
constLAZY_ATTRS=[
"data-src",
"data-srcset",
"data-original",
"data-lazy",
"data-thumb",
];
constisStylesheetLike=(el)=>{
if(el.tagName!=="LINK")returnfalse;
constrel=(el.getAttribute("rel")||"").toLowerCase();
constas=(el.getAttribute("as")||"").toLowerCase();
return(
rel==="stylesheet"||
(rel==="preload"&&(as==="style"||as==="script"))
);
};
functionshouldProcess(el){
constset=tagMap.get(el.tagName);
if(!set)returnfalse;
if(el.tagName==="LINK")returnisStylesheetLike(el);
returntrue;
}
functionrewriteAttributes(el){
if(!shouldProcess(el))return;
constattrs=tagMap.get(el.tagName);
// Primary attrs
attrs.forEach((a)=>{
if(!el.hasAttribute(a))return;
// Skip rel/as value rewrites except we read them (no replacement for rel/as)
if(el.tagName==="LINK"&&(a==="rel"||a==="as"))return;
constv=el.getAttribute(a);
constnv=replaceVal(v);
if(nv!==v)el.setAttribute(a,nv);
});
// Lazy attrs
LAZY_ATTRS.forEach((a)=>{
if(!el.hasAttribute(a))return;
constv=el.getAttribute(a);
constnv=replaceVal(v);
if(nv!==v)el.setAttribute(a,nv);
});
// srcset is a string; simple replace works
if(el.hasAttribute("srcset")){
constv=el.getAttribute("srcset");
constnv=replaceVal(v);
if(nv!==v)el.setAttribute("srcset",nv);
}
// Inline style attribute
if(swapInlineCSS&&el.hasAttribute("style")){
constv=el.getAttribute("style");
constnv=replaceVal(v);
if(nv!==v)el.setAttribute("style",nv);
}
}
functionrewriteStyleTag(styleEl){
if(!processStyleTag)return;
constv=styleEl.textContent||"";
constnv=replaceVal(v);
if(nv!==v)styleEl.textContent=nv;
}
functionrewriteAnchorsInMenus(root=document){
if(!menuSelectors.length)return;
// Only rewrite href on anchors matching the provided selectors
constanchors=menuSelectors.flatMap((sel)=>
Array.from(root.querySelectorAll(sel))
);
anchors.forEach((a)=>{
if(!(ainstanceofHTMLAnchorElement))return;
if(!a.hasAttribute("href"))return;
constv=a.getAttribute("href");
constnv=replaceVal(v);
if(nv!==v)a.setAttribute("href",nv);
});
}
functionfullScan(root=document.documentElement){
constwalker=document.createTreeWalker(
root,
NodeFilter.SHOW_ELEMENT,
null
);
letnode;
while((node=walker.nextNode())){
rewriteAttributes(node);
if(node.tagName==="STYLE")rewriteStyleTag(node);
}
rewriteAnchorsInMenus(document);
}
// Initial passes
fullScan();
if(document.readyState==="loading"){
document.addEventListener("DOMContentLoaded",()=>fullScan(),{
once: true,
});
}else{
fullScan();
}
// Observe mutations
constmo=newMutationObserver((muts)=>{
for(constmofmuts){
if(m.type==="attributes"){
// If the changed attr is relevant to asset swapping, process the element
rewriteAttributes(m.target);
}elseif(m.type==="childList"){
m.addedNodes.forEach((n)=>{
if(n.nodeType!==1)return;
rewriteAttributes(n);
if(n.tagName==="STYLE")rewriteStyleTag(n);
// Subtree
constsub=document.createTreeWalker(
n,
NodeFilter.SHOW_ELEMENT,
null
);
lete;
while((e=sub.nextNode())){
rewriteAttributes(e);
if(e.tagName==="STYLE")rewriteStyleTag(e);
}
// Menu links potentially added dynamically
rewriteAnchorsInMenus(n);
});
}
}
});
mo.observe(document.documentElement,{
subtree: true,
childList: true,
attributes: true,
attributeFilter: [
"src",
"srcset",
"href",
"poster",
"style",
"data-src",
"data-srcset",
"data-original",
"data-lazy",
"data-thumb",
"rel",
"as",
],
});
})();