Skip to content

Commit

Permalink
Updated a few more things
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiteAFancyEmerald committed Feb 8, 2022
1 parent c827e9f commit 73cdcb3
Show file tree
Hide file tree
Showing 47 changed files with 35,789 additions and 1,086 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "src/corrosion"]
path = src/corrosion
url = https://github.com/titaniumnetwork-dev/Corrosion

[submodule "views/archive/g/house"]
path = views/archive/g/house
url = https://github.com/razh/game-off-2013
Expand Down
69 changes: 69 additions & 0 deletions lib/browser/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { overrideAccessors } = require("./utils");

function createAttributeRewriter(ctx) {
if (ctx.serviceWorker) return () => null;
const {
HTMLMediaElement,
HTMLScriptElement,
HTMLAudioElement,
HTMLVideoElement,
HTMLInputElement,
HTMLEmbedElement,
HTMLTrackElement,
HTMLAnchorElement,
HTMLIFrameElement,
HTMLAreaElement,
HTMLLinkElement,
HTMLBaseElement,
HTMLFormElement,
HTMLImageElement,
HTMLSourceElement,
} = ctx.window;
function rewriteAttribute(elem, attr, handler) {
if (Array.isArray(elem)) {
elem.forEach(elem => rewriteAttribute(elem, attr, handler));
return true;
};
if (!elem.prototype || !elem.prototype.hasOwnProperty(attr)) return;
const proto = elem.prototype;
overrideAccessors(proto, attr, {
getter: (target, that) => {
const val = target.call(that);
switch(handler) {
case 'url':
return ctx.url.unwrap(val, ctx.meta);
case 'srcset':
return ctx.html.unsrcset(val, ctx.meta);
case 'delete':
return ctx.originalFn.elementGetAttribute.call(that, `corrosion-attr`) || '';
default:
return val;
};
},
setter: (target, that, [ val ]) => {
switch(handler) {
case 'url':
return target.call(that, ctx.url.wrap(val, ctx.meta));
case 'srcset':
return target.call(that, ctx.html.srcset(val, ctx.meta));
case 'delete':
ctx.originalFn.elementSetAttribute.call(that, `corrosion-attr`, val);
return val;
default:
return target.call(that, val);
};
},
});
return true;
};
return function rewriteAttributes() {
rewriteAttribute([ HTMLScriptElement, HTMLMediaElement, HTMLImageElement, HTMLAudioElement, HTMLVideoElement, HTMLInputElement, HTMLEmbedElement, HTMLIFrameElement, HTMLTrackElement, HTMLSourceElement ], 'src', 'url');
rewriteAttribute(HTMLFormElement, 'action', 'url');
rewriteAttribute([ HTMLAnchorElement, HTMLAreaElement, HTMLLinkElement, HTMLBaseElement ], 'href', 'url');
rewriteAttribute([ HTMLImageElement, HTMLSourceElement ], 'srcset', 'srcset');
rewriteAttribute(HTMLScriptElement, 'integrity', 'delete');
return true;
};
};

module.exports = createAttributeRewriter;
318 changes: 64 additions & 254 deletions lib/browser/document.js

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions lib/browser/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const { overrideAccessors, overrideFunction, overrideConstructor } = require("./utils");

function createDomRewriter(ctx) {
if (ctx.serviceWorker) return () => null;
if (ctx.window.Node && ctx.window.Node.prototype) {
ctx.originalAccessors.nodeBaseURI = Object.getOwnPropertyDescriptor(ctx.window.Node.prototype, 'baseURI');
ctx.originalAccessors.nodeTextContent = Object.getOwnPropertyDescriptor(ctx.window.Node.prototype, 'textContent');
};
if (ctx.window.Element && ctx.window.Element.prototype) {
ctx.originalAccessors.elementInnerHtml = Object.getOwnPropertyDescriptor(ctx.window.Element.prototype, 'innerHTML');
ctx.originalAccessors.elementOuterHtml = Object.getOwnPropertyDescriptor(ctx.window.Element.prototype, 'outerHTML');
ctx.originalFn.elementSetAttribute = ctx.window.Element.prototype.setAttribute;
ctx.originalFn.elementGetAttribute = ctx.window.Element.prototype.getAttribute;
ctx.originalFn.elementHasAttribute = ctx.window.Element.prototype.hasAttribute;
};
if (ctx.window.Audio) ctx.originalFn.Audio = ctx.window.Audio;
function rewriteTextContent() {
if (ctx.originalAccessors.nodeTextContent) {
overrideAccessors(ctx.window.Node.prototype, 'textContent', {
setter: (target, that, [ val ]) => {
switch(that.tagName) {
case 'SCRIPT':
val = ctx.processScript(val);
break;
case 'STYLE':
val = ctx.processStyle(val);
break;
};
return target.call(that, val);
},
});
};
};
function rewriteUrl() {
if (ctx.originalAccessors.nodeBaseURI) {
overrideAccessors(ctx.window.Node.prototype, 'baseURI', {
getter: (target, that) => {
const url = target.call(that);
return url.startsWith(ctx.meta.origin) ? ctx.url.unwrap(url, ctx.meta) : url;
},
});
};
};
function rewriteHtml() {
if (ctx.originalAccessors.elementInnerHtml) {
overrideAccessors(ctx.window.Element.prototype, 'innerHTML', {
getter: (target, that) => ['STYLE', 'SCRIPT'].includes(that.tagName) ? target.call(that) : ctx.html.source(target.call(that)),
setter: (target, that, [ val ]) => {
switch(that.tagName) {
case 'STYLE':
val = ctx.processStyle(val);
break;
case 'SCRIPT':
val = ctx.processScript(val);
break;
default:
val = ctx.processHtml(val);
break;
};
return target.call(that, val);
},
});
};
};
function rewriteAttribute() {
if (ctx.originalFn.elementSetAttribute) {
overrideFunction(ctx.window.Element.prototype, 'setAttribute', (target, that, args) => {
if (args[0] && args[1]) {
let data = {
attr: {
name: args[0],
value: args[1],
},
node: that,
meta: ctx.meta,
setAttribute: ctx.originalFn.elementSetAttribute.bind(that),
delete: false,
ctx,
};
const tag = ctx.html.attrs.get(that.tagName.toLowerCase()) || ctx.html.attrs.get('*');
if (tag[data.attr.name]) tag[data.attr.name](that, data);
args[0] = data.attr.name;
args[1] = data.attr.value;
};
return target.apply(that, args);
});
};
if (ctx.originalFn.elementGetAttribute) {
overrideFunction(ctx.window.Element.prototype, 'getAttribute', (target, that, args) => {
if (args[0] && ctx.originalFn.elementHasAttribute.call(that, `corrosion-attr-${args[0]}`)) args[0] = `corrosion-attr-${args[0]}`;
return target.apply(that, args);
});
};
};
function rewriteAudio() {
if (ctx.originalFn.Audio) {
overrideConstructor(ctx.window, 'Audio', (target, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
return new target(...args);
});
};
};
return function rewriteDom() {
rewriteUrl();
rewriteTextContent();
rewriteHtml();
rewriteAttribute();
rewriteAudio();
return true;
};
};

module.exports = createDomRewriter;
70 changes: 70 additions & 0 deletions lib/browser/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { overrideFunction, overrideAccessors, overrideConstructor, wrapFnString } = require("./utils");

function createFunctionRewriter(ctx) {
if (ctx.window.Function && ctx.window.Function.prototype) {
ctx.originalFn.Function = ctx.window.Function;
ctx.originalFn.FunctionToString = ctx.window.Function.prototype.toString;
ctx.originalAccessors.FunctionArguments = Object.getOwnPropertyDescriptor(ctx.window.Function.prototype, 'arguments');
};
function rewriteFn() {
if (ctx.originalFn.Function) {
//const fnProto = ctx.window.Function.prototype;
/*overrideFunction(ctx.window, 'Function', (target, that, args) => {
if (args.length) args[args.length - 1] = ctx.js.process(args[args.length - 1], ctx.meta.url);
return target.apply(that, args);
});*/
/*ctx.window.Function = new Proxy(ctx.window.Function, {
apply: (target, that, args) => {
if (args.length) args[args.length - 1] = ctx.processScript(args[args.length - 1]);
return target.apply(that, args);
},
construct: (target, args) => {
if (args.length) args[args.length - 1] = ctx.processScript(args[args.length - 1]);
return new target(...args);
},
get: (target, prop) => target[prop],
set: (target, prop, val) => target[prop] = val,
});*/
/*
ctx.window.Function = function(...args) {
if (args.length) args[args.length - 1] = ctx.processScript(args[args.length - 1]);
return new ctx.originalFn.Function(...args);
};
ctx.window.Function.prototype = ctx.originalFn.Function.prototype;
ctx.window.Function.prototype.constructor = ctx.window.Function;
wrapFnString(ctx.originalFn.Function, ctx.window.Function);
*/
overrideConstructor(ctx.window, 'Function', (target, args) => {
const old = args[args.length - 1];
if (args.length) args[args.length - 1] = ctx.processScript(args[args.length - 1]);
console.log(old, args[args.length - 1]);
return target.apply(this, args);
});
};
return true;
};
function rewriteFnArguments() {
if (ctx.originalAccessors.FunctionArguments) {
overrideAccessors(ctx.window.Function.prototype, 'arguments', {
getter: (target, that) => target.call(ctx.proxyToOriginal(that)),
});
};
return true;
};
function rewriteFnString() {
if (ctx.originalFn.FunctionToString) {
overrideFunction(ctx.window.Function.prototype, 'toString', (target, that, args) => {
if (that.hasOwnProperty('$corrosion_string')) return that.$corrosion_string;
return target.apply(that, args);
});
};
return true;
};
return function rewriteFunction() {
rewriteFnString();
rewriteFn();
//rewriteFnArguments();
};
};

module.exports = createFunctionRewriter;
46 changes: 25 additions & 21 deletions lib/browser/history.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
const { overrideFunction } = require("./utils");

function createHistoryRewriter(ctx) {
if (ctx.serviceWorker) return () => null;
if (ctx.window.History && ctx.window.History.prototype) {
ctx.originalFn.historyPushstate = ctx.window.History.prototype.pushState;
ctx.originalFn.historyReplacestate = ctx.window.History.prototype.replaceState;
};
function rewritePushReplaceState() {
const handler = (target, that, args) => {
if (args[2]) {
/*if (new URL(args[2], ctx.meta.base).origin != ctx.location.origin) {
args[2] = '';
} else {*/
args[2] = ctx.url.wrap(args[2], ctx.meta);
//};
};
return target.apply(that, args);
}
if (ctx.originalFn.historyPushstate) overrideFunction(ctx.window.History.prototype, 'pushState', handler);
if (ctx.originalFn.historyReplacestate) overrideFunction(ctx.window.History.prototype, 'replaceState', handler);
return true;
};
return function rewriteHistory() {
if (ctx.serviceWorker) return;
if (ctx.window.History.prototype.pushState) {
ctx.window.History.prototype.pushState = new Proxy(ctx.window.History.prototype.pushState, {
apply: (target, that, args) => {
if (args[2]) args[2] = ctx.url.wrap(args[2], ctx.meta);
const ret = Reflect.apply(target, that, args);
ctx.updateLocation();
return ret;
},
});
};
if (ctx.window.History.prototype.replaceState) {
ctx.window.History.prototype.replaceState = new Proxy(ctx.window.History.prototype.replaceState, {
apply: (target, that, args) => {
if (args[2]) args[2] = ctx.url.wrap(args[2], ctx.meta);
const ret = Reflect.apply(target, that, args);
ctx.updateLocation();
return ret;
},
});
};
rewritePushReplaceState();
return true;
};
};

module.exports = createHistoryRewriter;
Loading

0 comments on commit 73cdcb3

Please sign in to comment.