1

I'm attempting to modify an object present in code that I do not control and cannot directly alter. Ideally, I'd like to do this from a bookmarklet, but the extra permissions of a userscript are also a usable context.

The code I'm attempting to hook looks like this:

(() => {
  var target = { nestedsubobject: { foo: 'value' } };
  // does some stuff with target
})();

I'd like to intercept the object literal assigned to target and modify the value of foo, before the IIFE uses it further.

So far everything I've tried that doesn't work:

  • Prototypes appear unrelated to this problem. While I can override the global object prototype, this gives me no way to actually listen for objects created using that prototype.
    • Using Object.defineProperty on Object.prototype to define nestedsubobject or foo with writable: false or get() results in the client code throwing TypeError: can't redefine non-configurable property (...) when constructing the target object. Configuring with value just results in the property being present in the new object and thus ignored in the prototype object.
  • Constructors are bypassed with object literals. While I can override the global object constructor (as in Override JavaScript global native Object() constructor), the literal syntax doesn't appear to use that constructor.

Additionally, I can't alter the code directly (e.g. intercepting and rewriting the network request for it) because I expect that it may be obfuscated in the future. I'd like to directly hook object creation to make my userscript much more durable.

This is running in a browser context.

5
  • It would be a security violation if developers could proxy construction of objects like that. Commented Mar 25, 2023 at 20:55
  • Is this code running in a browser context? Commented Mar 25, 2023 at 20:56
  • @trincot this is running in a browser, I've edited the question to clarify that. Commented Mar 25, 2023 at 21:05
  • Does this answer your question? Stop execution of Javascript function (client side) or tweak it Commented Mar 27, 2023 at 14:36
  • @double-beep unfortunately not quite. It'd work in a similar situation, but I specifically need to depend on the shape of the resulting object (which I expect to remain mostly stable) and not modify the code responsible for creating it directly (which I expect will be possibly unstable, which I wish to futureproof against potential obfuscation, and which I have further reasons for not modifying that I can't share here) Commented Mar 31, 2023 at 2:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.