zorapeteri / bookmarks-to-json

Convert browser-exported HTML bookmarks to JSON format
https://zorapeteri.github.io/bookmarks-to-json/
MIT License
27 stars 2 forks source link

I can't get it to work in Vue? #1

Closed presencewebdesign closed 1 year ago

presencewebdesign commented 1 year ago

I keep getting this error Error in v-on handler: "TypeError: fs.readFileSync is not a function"

<input @change="onFileChanged" type="file" />

onFileChanged() {
  const content = fs.readFileSync('./public/file.html', 'utf-8')
  const options = {
    formatJSON: true, // return prettified JSON - false by default
    spaces: 2 // number of spaces to use for indentation - 2 by default
  }
  fs.writeFileSync('bookmarks.json', bookmarksToJSON(content, options))
},
zorapeteri commented 1 year ago

Hey @presencewebdesign,

The example I have provided works with fs which is the file system handling import in Node.

To make it work on the frontend, you'll have to find a way to get the contents of the file and then pass that string to bookmarksToJSON. I'm no Vue expert, so the one solution I can think of is making a fetch request to get the contents of the file if you know the path it's publicly available at, but there might be better ways to do this.

Also just want to note that just as readFileSync does, writeFileSync will also fail as you're in frontend code, but I imagine you don't actually want to write into a file somewhere, so I think it would be better for you to first change my example to the way you would actually want to work with the result, and see if you run into any issues (fingers crossed you don't).

Let me know if this was of any help