Skip to content

Commit

Permalink
Simplified code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kkapsner committed Dec 12, 2019
1 parent 0d331d9 commit af1dfe7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 95 deletions.
191 changes: 96 additions & 95 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,112 +315,113 @@
const changedFunction = changedFunctions[name];
const functionStatus = changedFunction.getStatus(undefined, siteStatus, prefs);
logging.verbose("status for", name, ":", functionStatus);
if (functionStatus.active){
getAllFunctionObjects(windowToProcess, changedFunction).forEach(function(object){
if (object){
const original = object[name];
const checker = generateChecker({
name, changedFunction, siteStatus, original,
window: windowToProcess, prefs, checkStack, ask, notify
});
const descriptor = Object.getOwnPropertyDescriptor(object, name);
if (descriptor){
if (descriptor.hasOwnProperty("value")){
if (changedFunction.fakeGenerator){
descriptor.value = exportFunction(
changedFunction.fakeGenerator(checker, original, windowToProcess),
windowToProcess
);
}
else {
descriptor.value = null;
}
}
else {
descriptor.get = exportFunction(function(){
return exportFunction(
changedFunction.fakeGenerator(checker),
windowToProcess
);
}, windowToProcess);
}
Object.defineProperty(object, name, descriptor);
}
}
if (!functionStatus.active) return;

getAllFunctionObjects(windowToProcess, changedFunction).forEach(function(object){
if (!object) return;

const original = object[name];
const checker = generateChecker({
name, changedFunction, siteStatus, original,
window: windowToProcess, prefs, checkStack, ask, notify
});
}
const descriptor = Object.getOwnPropertyDescriptor(object, name);
if (!descriptor) return;

if (descriptor.hasOwnProperty("value")){
if (changedFunction.fakeGenerator){
descriptor.value = extension.exportFunctionWithName(
changedFunction.fakeGenerator(checker, original, windowToProcess),
windowToProcess,
original.name
);
}
else {
descriptor.value = null;
}
}
else {
descriptor.get = extension.exportFunctionWithName(function(){
return extension.exportFunctionWithName(
changedFunction.fakeGenerator(checker),
windowToProcess,
original.name
);
}, windowToProcess, descriptor.get.name);
}
Object.defineProperty(object, name, descriptor);
});
});
}
function interceptGetters(windowToProcess, siteStatus, {checkStack, ask, notify, prefs}){
changedGetters.forEach(function(changedGetter){
const name = changedGetter.name;
const functionStatus = changedGetter.getStatus(undefined, siteStatus, prefs);
logging.verbose("status for", changedGetter, ":", functionStatus);
if (functionStatus.active){
changedGetter.objectGetters.forEach(function(objectGetter){
const object = objectGetter(extension.getWrapped(windowToProcess));
if (object){
const descriptor = Object.getOwnPropertyDescriptor(object, name);
if (descriptor && descriptor.hasOwnProperty("get")){
const original = descriptor.get;
const checker = generateChecker({
name, changedFunction: changedGetter, siteStatus, original,
window: windowToProcess, prefs, checkStack, ask, notify
});
const getter = changedGetter.getterGenerator(checker, original, windowToProcess);
descriptor.get = extension.exportFunctionWithName(getter, windowToProcess, original.name);

if (descriptor.hasOwnProperty("set") && descriptor.set && changedGetter.setterGenerator){
const original = descriptor.set;
const setter = changedGetter.setterGenerator(
windowToProcess,
original,
prefs
);
descriptor.set = extension.exportFunctionWithName(setter, windowToProcess, original.name);
}

Object.defineProperty(object, name, descriptor);
}
else if (
changedGetter.valueGenerator &&
descriptor && descriptor.hasOwnProperty("value")
){
const protectedAPIFeatures = prefs("protectedAPIFeatures");
if (
functionStatus.active &&
(
!protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) ||
protectedAPIFeatures[name + " @ " + changedGetter.api]
)
){
switch (functionStatus.mode){
case "ask": case "block": case "fake":
descriptor.value = changedGetter.valueGenerator({
mode: functionStatus.mode,
original: descriptor.value,
notify: function notifyCallback(messageId){
notify({
url: getURL(windowToProcess),
errorStack: (new Error()).stack,
messageId,
timestamp: new Date(),
functionName: name,
api: changedGetter.api
});
}
if (!functionStatus.active) return;

changedGetter.objectGetters.forEach(function(objectGetter){
const object = objectGetter(extension.getWrapped(windowToProcess));
if (!object) return;

const descriptor = Object.getOwnPropertyDescriptor(object, name);
if (!descriptor) return;

if (descriptor.hasOwnProperty("get")){
const original = descriptor.get;
const checker = generateChecker({
name, changedFunction: changedGetter, siteStatus, original,
window: windowToProcess, prefs, checkStack, ask, notify
});
const getter = changedGetter.getterGenerator(checker, original, windowToProcess);
descriptor.get = extension.exportFunctionWithName(getter, windowToProcess, original.name);

if (descriptor.hasOwnProperty("set") && descriptor.set && changedGetter.setterGenerator){
const original = descriptor.set;
const setter = changedGetter.setterGenerator(
windowToProcess,
original,
prefs
);
descriptor.set = extension.exportFunctionWithName(setter, windowToProcess, original.name);
}

Object.defineProperty(object, name, descriptor);
}
else if (
changedGetter.valueGenerator &&
descriptor.hasOwnProperty("value")
){
const protectedAPIFeatures = prefs("protectedAPIFeatures");
if (
!protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) ||
protectedAPIFeatures[name + " @ " + changedGetter.api]
){
switch (functionStatus.mode){
case "ask": case "block": case "fake":
descriptor.value = changedGetter.valueGenerator({
mode: functionStatus.mode,
original: descriptor.value,
notify: function notifyCallback(messageId){
notify({
url: getURL(windowToProcess),
errorStack: (new Error()).stack,
messageId,
timestamp: new Date(),
functionName: name,
api: changedGetter.api
});
Object.defineProperty(object, name, descriptor);
break;
}
}
}
else {
logging.error("Try to fake non getter property:", changedGetter);
}
});
Object.defineProperty(object, name, descriptor);
break;
}
}
});
}
}
else {
logging.error("Try to fake non getter property:", changedGetter);
}
});
});
}
scope.intercept = function intercept({subject: windowToProcess}, apis){
Expand Down
4 changes: 4 additions & 0 deletions versions/updates.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
{
"version": "0.5.15RC1",
"update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.15RC1-an+fx.xpi"
},
{
"version": "0.5.15RC2",
"update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.15RC2-an+fx.xpi"
}
]
}
Expand Down

0 comments on commit af1dfe7

Please sign in to comment.