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

Fixing a missing defensive check for an absent window global variable that was breaking service worker environments #443

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

rory-instil
Copy link
Contributor

Reusing the root variable defined in the file above - and used in a similar way on line 12 - we can enable service worker environments to use this library.
Otherwise these environments will encounter a window is not defined error.

This will close #397.

… that was breaking service worker environments
@morganhvidt
Copy link

This fixes my issue 🥳 I'm looking forward to the merge!

@nyacg
Copy link

nyacg commented Oct 7, 2023

I think this fixed my issue too, would be great to get it merged since it's approved

@martincizek martincizek merged commit 4afc328 into mixmark-io:master Oct 9, 2023
@makutamoto
Copy link

It seems this PR has not been released yet.
Could you release this?

@TranquilMarmot
Copy link

Any chance this could get released soon? 😁

@poltak
Copy link

poltak commented Mar 15, 2024

This was merged in a while back now but there still hasn't been a release. Any idea when this might happen?

Lots of web extensions are having to move to manifest V3 in the next few months and one of the big changes there on Chrome is that your extension's background script will now run as a Service Worker. Which lacks a window global and thus turndown stops working. This PR would fix turndown for MV3 web exts

@rory-instil
Copy link
Contributor Author

I've been using this pnpm patch / patch-package in lieu of a release: https://gist.github.com/rory-instil/2091eeeaf087d6caaff7c91a7e16f384

@martincizek
Copy link
Collaborator

Released v7.1.3. Please try it out and let me know if the issue is fixed.

@poltak
Copy link

poltak commented Mar 18, 2024

Just verified it fixes it for me in Chrome MV3 background service worker context. Thanks @martincizek !

@poltak
Copy link

poltak commented Mar 18, 2024

I spoke too soon without giving it a good enough test. After properly testing it, it errors out trying to access document global, which is not available in service workers, here:
https://github.com/mixmark-io/turndown/blob/master/src/html-parser.js#L42

I see you're using domino to afford HTML parsing in non-browser environments. I'm guessing this would also work in service workers, though I'm not sure how you could easily differentiate standard browser envs from service workers in a similar way to what you're currently doing to differentiate node from browser.

It would also probably also significantly increase the SW script size. An ideal solution would be to afford the user passing in their own HTML parsing implementation, as that may allow users to avoid the script size increase if they're already using some other non-browser HTML parsing solution, like cheerio. Of course, that's more work.

@martincizek
Copy link
Collaborator

It would also probably also significantly increase the SW script size.

There are different builds for browser and non-browser. I think you can influence this with how you declare the dependencies. And the online browser-build does not contain domino indeed.

@martincizek
Copy link
Collaborator

I spoke too soon without giving it a good enough test. After properly testing it, it errors out trying to access document global, which is not available in service workers, here: https://github.com/mixmark-io/turndown/blob/master/src/html-parser.js#L42

This code should not be accessed if you provide turndown() a DOM node yourself.

@poltak
Copy link

poltak commented Mar 21, 2024

Ah that's quite obvious in hindsight. Seems to work fine with a Document object produced via linkedom's worker mode. Thanks for the support @martincizek !

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.

Guard window object access for environments which don't have it
8 participants