From 200f6b31f3225b5ab33652bec69fc6fce278dfbc Mon Sep 17 00:00:00 2001 From: kkapsner Date: Fri, 29 Mar 2024 16:37:07 +0100 Subject: [PATCH] Do not use proxy for Function.prototype.toString Fixes #685 --- lib/extension.js | 18 ++++++++---------- releaseNotes.txt | 1 + test/detectionTest.js | 10 ++++++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/extension.js b/lib/extension.js index ac41b13..d4c92c3 100644 --- a/lib/extension.js +++ b/lib/extension.js @@ -144,20 +144,18 @@ const changedToStrings = new WeakMap(); scope.createProxyFunction = function createProxyFunction(window, original, replacement){ if (!changedToStrings.get(window)){ - changedToStrings.set(window, true); const functionPrototype = scope.getWrapped(window).Function.prototype; - const toString = functionPrototype.toString; + const originalToString = functionPrototype.toString; + changedToStrings.set(window, originalToString); + const alteredToString = scope.exportFunctionWithName(function toString(){ + return proxies.get(this) || originalToString.call(this); + }, window, "toString"); + proxies.set(alteredToString, originalToString.call(originalToString)); scope.changeProperty(window, "toString", { object: functionPrototype, name: "toString", type: "value", - changed: scope.createProxyFunction( - window, - toString, - function(){ - return proxies.get(this) || toString.call(this); - } - ) + changed: alteredToString }); } const handler = scope.getWrapped(window).Object.create(null); @@ -177,7 +175,7 @@ } }, window, ""); const proxy = new window.Proxy(original, handler); - proxies.set(proxy, original.toString()); + proxies.set(proxy, changedToStrings.get(window).call(original)); return scope.getWrapped(proxy); }; diff --git a/releaseNotes.txt b/releaseNotes.txt index fc73cb8..0553a11 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -1,6 +1,7 @@ Version 1.10: changes: - added eBay and facebook.com to the convenience preset + - do not use proxy for Function.prototype.toString (causes weird problems sometimes) new features: - added setting showPresetsOnInstallation to be able to not show the presets page upon installation diff --git a/test/detectionTest.js b/test/detectionTest.js index 4a9758f..411ee5e 100644 --- a/test/detectionTest.js +++ b/test/detectionTest.js @@ -181,6 +181,16 @@ addTest("toString modified", function(log){ configurable: true }, log + ) | checkPropertyDescriptor( + Function.prototype, + "toString", + { + value: function toString(){}, + writable: true, + enumerable: false, + configurable: true + }, + log ); }); addTest("function name", function(log){