Add onload hook

This commit is contained in:
the1812 2019-03-24 22:17:41 +08:00
parent a556cb2045
commit da64d2653d
5 changed files with 82 additions and 57 deletions

View File

@ -1,6 +1,6 @@
// ==UserScript== // ==UserScript==
// @name Bilibili Evolved (Offline) // @name Bilibili Evolved (Offline)
// @version 250.89 // @version 251.27
// @description Bilibili Evolved 的离线版, 所有功能都已内置于脚本中. // @description Bilibili Evolved 的离线版, 所有功能都已内置于脚本中.
// @author Grant Howard, Coulomb-G // @author Grant Howard, Coulomb-G
// @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G)
@ -293,27 +293,32 @@ function setupAjaxHook()
fireHandlers("after" + name, thisArgs, ...args); fireHandlers("after" + name, thisArgs, ...args);
return returnValue; return returnValue;
}; };
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); }; const hookOnEvent = (name, thisArg) =>
XMLHttpRequest.prototype.send = function (...args)
{ {
if (this.onreadystatechange) if (thisArg[name])
{ {
const originalHandler = this.onreadystatechange; const originalHandler = thisArg[name];
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
originalHandler.apply(this, args); originalHandler.apply(thisArg, args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
else else
{ {
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
};
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); };
XMLHttpRequest.prototype.send = function (...args)
{
hookOnEvent("onreadystatechange", this);
hookOnEvent("onload", this);
return hook("send", this, ...args); return hook("send", this, ...args);
}; };
} }

View File

@ -1,6 +1,6 @@
// ==UserScript== // ==UserScript==
// @name Bilibili Evolved (Preview Offline) // @name Bilibili Evolved (Preview Offline)
// @version 250.89 // @version 251.27
// @description Bilibili Evolved 的预览离线版, 可以抢先体验新功能, 并且所有功能都已内置于脚本中. // @description Bilibili Evolved 的预览离线版, 可以抢先体验新功能, 并且所有功能都已内置于脚本中.
// @author Grant Howard, Coulomb-G // @author Grant Howard, Coulomb-G
// @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G)
@ -293,27 +293,32 @@ function setupAjaxHook()
fireHandlers("after" + name, thisArgs, ...args); fireHandlers("after" + name, thisArgs, ...args);
return returnValue; return returnValue;
}; };
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); }; const hookOnEvent = (name, thisArg) =>
XMLHttpRequest.prototype.send = function (...args)
{ {
if (this.onreadystatechange) if (thisArg[name])
{ {
const originalHandler = this.onreadystatechange; const originalHandler = thisArg[name];
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
originalHandler.apply(this, args); originalHandler.apply(thisArg, args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
else else
{ {
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
};
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); };
XMLHttpRequest.prototype.send = function (...args)
{
hookOnEvent("onreadystatechange", this);
hookOnEvent("onload", this);
return hook("send", this, ...args); return hook("send", this, ...args);
}; };
} }

View File

@ -292,27 +292,32 @@ function setupAjaxHook()
fireHandlers("after" + name, thisArgs, ...args); fireHandlers("after" + name, thisArgs, ...args);
return returnValue; return returnValue;
}; };
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); }; const hookOnEvent = (name, thisArg) =>
XMLHttpRequest.prototype.send = function (...args)
{ {
if (this.onreadystatechange) if (thisArg[name])
{ {
const originalHandler = this.onreadystatechange; const originalHandler = thisArg[name];
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
originalHandler.apply(this, args); originalHandler.apply(thisArg, args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
else else
{ {
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
};
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); };
XMLHttpRequest.prototype.send = function (...args)
{
hookOnEvent("onreadystatechange", this);
hookOnEvent("onload", this);
return hook("send", this, ...args); return hook("send", this, ...args);
}; };
} }

View File

@ -292,27 +292,32 @@ function setupAjaxHook()
fireHandlers("after" + name, thisArgs, ...args); fireHandlers("after" + name, thisArgs, ...args);
return returnValue; return returnValue;
}; };
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); }; const hookOnEvent = (name, thisArg) =>
XMLHttpRequest.prototype.send = function (...args)
{ {
if (this.onreadystatechange) if (thisArg[name])
{ {
const originalHandler = this.onreadystatechange; const originalHandler = thisArg[name];
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
originalHandler.apply(this, args); originalHandler.apply(thisArg, args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
else else
{ {
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
};
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); };
XMLHttpRequest.prototype.send = function (...args)
{
hookOnEvent("onreadystatechange", this);
hookOnEvent("onload", this);
return hook("send", this, ...args); return hook("send", this, ...args);
}; };
} }

View File

@ -96,27 +96,32 @@ export function setupAjaxHook()
fireHandlers("after" + name, thisArgs, ...args); fireHandlers("after" + name, thisArgs, ...args);
return returnValue; return returnValue;
}; };
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); }; const hookOnEvent = (name, thisArg) =>
XMLHttpRequest.prototype.send = function (...args)
{ {
if (this.onreadystatechange) if (thisArg[name])
{ {
const originalHandler = this.onreadystatechange; const originalHandler = thisArg[name];
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
originalHandler.apply(this, args); originalHandler.apply(thisArg, args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
else else
{ {
this.onreadystatechange = (...args) => thisArg[name] = (...args) =>
{ {
fireHandlers("beforeOnReadyStateChange", this, ...args); fireHandlers("before" + name, thisArg, ...args);
fireHandlers("afterOnReadyStateChange", this, ...args); fireHandlers("after" + name, thisArg, ...args);
}; };
} }
};
XMLHttpRequest.prototype.open = function (...args) { return hook("open", this, ...args); };
XMLHttpRequest.prototype.send = function (...args)
{
hookOnEvent("onreadystatechange", this);
hookOnEvent("onload", this);
return hook("send", this, ...args); return hook("send", this, ...args);
}; };
} }