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

Guard window object access for environments which don't have it #397

Closed
pilotmoon opened this issue Aug 31, 2021 · 5 comments · Fixed by #443
Closed

Guard window object access for environments which don't have it #397

pilotmoon opened this issue Aug 31, 2021 · 5 comments · Fixed by #443

Comments

@pilotmoon
Copy link

pilotmoon commented Aug 31, 2021

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

I'm running turndown in a macOS app using JavaScriptCore, which is an environment without a window object. Turndown works great except for one line where it reference the window object. It was a simple fix to add a guard expression.

Here is the diff that solved my problem:

diff --git a/node_modules/turndown/dist/turndown.js b/node_modules/turndown/dist/turndown.js
index 9ea22ec..04d6a6d 100644
--- a/node_modules/turndown/dist/turndown.js
+++ b/node_modules/turndown/dist/turndown.js
@@ -609,7 +609,7 @@ var TurndownService = (function () {
     try {
       document.implementation.createHTMLDocument('').open();
     } catch (e) {
-      if (window.ActiveXObject) useActiveX = true;
+      if (typeof window !== 'undefined' && window.ActiveXObject) useActiveX = true;
     }
     return useActiveX
   }
diff --git a/node_modules/turndown/lib/turndown.browser.cjs.js b/node_modules/turndown/lib/turndown.browser.cjs.js
index a7490d3..16af063 100644
--- a/node_modules/turndown/lib/turndown.browser.cjs.js
+++ b/node_modules/turndown/lib/turndown.browser.cjs.js
@@ -608,7 +608,7 @@ function shouldUseActiveX () {
   try {
     document.implementation.createHTMLDocument('').open();
   } catch (e) {
-    if (window.ActiveXObject) useActiveX = true;
+    if (typeof window !== 'undefined' && window.ActiveXObject) useActiveX = true;
   }
   return useActiveX
 }
diff --git a/node_modules/turndown/lib/turndown.browser.es.js b/node_modules/turndown/lib/turndown.browser.es.js
index f3c118e..6d75f56 100644
--- a/node_modules/turndown/lib/turndown.browser.es.js
+++ b/node_modules/turndown/lib/turndown.browser.es.js
@@ -606,7 +606,7 @@ function shouldUseActiveX () {
   try {
     document.implementation.createHTMLDocument('').open();
   } catch (e) {
-    if (window.ActiveXObject) useActiveX = true;
+    if (typeof window !== 'undefined' && window.ActiveXObject) useActiveX = true;
   }
   return useActiveX
 }
diff --git a/node_modules/turndown/lib/turndown.browser.umd.js b/node_modules/turndown/lib/turndown.browser.umd.js
index a812101..2c6542c 100644
--- a/node_modules/turndown/lib/turndown.browser.umd.js
+++ b/node_modules/turndown/lib/turndown.browser.umd.js
@@ -612,7 +612,7 @@
     try {
       document.implementation.createHTMLDocument('').open();
     } catch (e) {
-      if (window.ActiveXObject) useActiveX = true;
+      if (typeof window !== 'undefined' && window.ActiveXObject) useActiveX = true;
     }
     return useActiveX
   }

This issue body was partially generated by patch-package.

@Radiergummi
Copy link

@ccordoui would you accept a PR that fixes this single line preventing Turndown from running in web workers?

@ccordoui
Copy link

@Radiergummi it will patch the case of the undefined window, unfortunately as we don't have access to DOM in a service worker, it would need more work to be fully functional.

I ended up repeating the html to markdown process in all part of my web extension and the service worker only communicate in json.

@pilotmoon
Copy link
Author

Turndown can be used in DOM-less environments without further modification by using a DOM library such as linkedom.

@laike9m
Copy link

laike9m commented Jun 8, 2023

Met the same issue when trying to use turndown in a service worker

@rory-instil
Copy link
Contributor

I have the same issue - I had to fork the library and fix it with:

if (root.ActiveXObject) useActiveX = true;

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