mixmark-io / turndown

πŸ› An HTML to Markdown converter written in JavaScript
https://mixmark-io.github.io/turndown
MIT License
8.62k stars 870 forks source link

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

Closed pilotmoon closed 11 months ago

pilotmoon commented 3 years ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch turndown@7.1.1 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 commented 1 year ago

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

ccordoui commented 1 year ago

@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 commented 1 year ago

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

laike9m commented 1 year ago

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

rory-instil commented 1 year ago

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

if (root.ActiveXObject) useActiveX = true;