Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for window override #3087

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dickeylth
Copy link

In my case I want to add Proxy to the window object to intercept property access to window from js code, however I find it almost impossible to override the window object with the new object which is wrapped by Proxy. So I suggest maybe through the return value of options.beforeParse, it could return a new override window object?

Suggest usage like following:

const $ = new JSDOM(`<!DOCTYPE html><html>${htmlCode}</html>`, {
    runScripts: "dangerously",
    pretendToBeVisual: true
    beforeParse(window) {
      const handler = {
        get: function(obj, prop) {
            console.log(`reading window property: ${prop.toString()}`);
            return obj[prop];
        },
        getOwnPropertyDescriptor(target, prop) {
          console.log(`reading getOwnPropertyDescriptor ${prop}`);
          return target[prop];
        }
      };
      return new Proxy(window, handler);
    }
  }
);

Or maybe is there any more reasonable solution for this situation?

In my case I want to add Proxy to the `window` object to intercept property access to `window` from js code, however I find it almost impossible to override the `window` object with the new object which is wrapped by `Proxy`. So I suggest maybe through the return value of `options.beforeParse`, it could return a new override `window` object?

Suggest usage like following:

```js
const $ = new JSDOM(`<!DOCTYPE html><html>${htmlCode}</html>`, {
    runScripts: "dangerously",
    pretendToBeVisual: true
    beforeParse(window) {
      const handler = {
        get: function(obj, prop) {
            console.log(`reading window property: ${prop.toString()}`);
            return obj[prop];
        },
        getOwnPropertyDescriptor(target, prop) {
          console.log(`reading getOwnPropertyDescriptor ${prop}`);
          return target[prop];
        }
      };
      return new Proxy(window, handler);
    }
  }
);
```
@dickeylth dickeylth closed this Dec 27, 2020
@dickeylth dickeylth reopened this Dec 27, 2020
@dickeylth dickeylth closed this May 18, 2021
@dickeylth dickeylth reopened this May 18, 2021
@lenqwang
Copy link

lenqwang commented Nov 17, 2021

What's this PR is going on? I recently need this feature in the same scene with you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants