Skip to content

Commit 89285ac

Browse files
authored
添加history和setAttribute注入
1 parent 74bf938 commit 89285ac

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

_worker.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var mainOnly = path.substring(0, path.indexOf("://")) + "://" + original_host +
6060
6161
//---***========================================***---通用func---***========================================***---
6262
function changeURL(relativePath){
63+
if(relativePath == null) return null;
6364
if(relativePath.startsWith("data:") || relativePath.startsWith("mailto:") || relativePath.startsWith("javascript:") || relativePath.startsWith("chrome") || relativePath.startsWith("edge")) return relativePath;
6465
try{
6566
if(relativePath && relativePath.startsWith(nowlink)) relativePath = relativePath.substring(nowlink.length);
@@ -88,7 +89,7 @@ function changeURL(relativePath){
8889
absolutePath = nowlink + absolutePath;
8990
return absolutePath;
9091
} catch (e) {
91-
console.log(path + " " + relativePath);
92+
console.log("Exception occured: " + e.message + path + " " + relativePath);
9293
return "";
9394
}
9495
}
@@ -172,6 +173,24 @@ console.log("APPEND CHILD INJECTED");
172173
}
173174
174175
176+
177+
178+
//---***========================================***---注入元素的src和href---***========================================***---
179+
function elementPropertyInject(){
180+
const originalSetAttribute = HTMLElement.prototype.setAttribute;
181+
HTMLElement.prototype.setAttribute = function (name, value) {
182+
if (name == "src" || name == "href") {
183+
value = changeURL(value);
184+
//console.log("~~~~~~" + value);
185+
}
186+
originalSetAttribute.call(this, name, value);
187+
};
188+
console.log("ELEMENT PROPERTY (new Proxy) INJECTED");
189+
}
190+
191+
192+
193+
175194
//---***========================================***---注入location---***========================================***---
176195
class ProxyLocation {
177196
constructor(originalLocation) {
@@ -333,17 +352,27 @@ function historyInject(){
333352
const originalReplaceState = History.prototype.replaceState;
334353
335354
History.prototype.pushState = function (state, title, url) {
336-
var u = new URL(url, now.href).href;
355+
var u = changeURL(url);
337356
return originalPushState.apply(this, [state, title, u]);
338357
};
358+
339359
History.prototype.replaceState = function (state, title, url) {
340-
console.log(nowlink);
341-
console.log(url);
342-
console.log(now.href);
343-
var u = new URL(url, now.href).href;
344-
console.log(u);
360+
var u = changeURL(url);
345361
return originalReplaceState.apply(this, [state, title, u]);
346362
};
363+
364+
History.prototype.back = function () {
365+
return originalBack.apply(this);
366+
};
367+
368+
History.prototype.forward = function () {
369+
return originalForward.apply(this);
370+
};
371+
372+
History.prototype.go = function (delta) {
373+
return originalGo.apply(this, [delta]);
374+
};
375+
347376
console.log("HISTORY INJECTED");
348377
}
349378
@@ -394,10 +423,9 @@ function covToAbs(element) {
394423
if (!relativePath.includes("*")) {
395424
try {
396425
var absolutePath = changeURL(relativePath);
397-
console.log(absolutePath);
398426
element.setAttribute(setAttr, absolutePath);
399427
} catch (e) {
400-
console.log(path + " " + relativePath);
428+
console.log("Exception occured: " + e.message + path + " " + relativePath);
401429
}
402430
}
403431
}
@@ -454,11 +482,11 @@ function covScript(){ //由于observer经过测试不会hook添加的script标
454482
//---***========================================***---操作---***========================================***---
455483
networkInject();
456484
windowOpenInject();
457-
appendChildInject();
485+
elementPropertyInject();
486+
//appendChildInject(); 经过测试如果放上去将导致maps.google.com无法使用
458487
documentLocationInject();
459488
windowLocationInject();
460-
// historyInject();
461-
// 这里实在无能为力不想改,可以pr一个
489+
historyInject();
462490
463491
464492

0 commit comments

Comments
 (0)