1

I'm creating a game using vanilla javascript (html/css/js), and I'm using GitHub Pages to host the website. The problem is that I'm not able to understand how I can edit a file (scores.json) through javascript to save player scores. I can access the file and use the data, but I cannot modify it.

Edit: I practically haven't tested anything yet, because I haven't been able to find any useful information online, and I'm not very knowledgeable about reading and writing files on a website. The only thing I tested was this https://stackoverflow.com/a/21016088/13973113 which is not what I'm looking for, because it's the user saving the file locally and not the JavaScript to the repository. I'm also not familiarized with database which are probably a better solution for this.

Files:

index.html (homepage)
game/
├── assets/
│   └── scores.json
├── index.html (game page)
└── game.js
TxFig
  • 13
  • 3
  • 1
    Hi, welcome to SO! What did you try so far? – Camilo Aug 06 '22 at 19:33
  • 1
    You can't physically edit it with a purely client-side app like that. You'll need to either store scores in local storage, use a database to store scores, or if you really have to edit that file, then a writing GitHub Action would be the only option. – Alicia Sykes Aug 06 '22 at 19:34
  • 1
    Also, to get a good answer, you need to provide code snippets (minimal reproducible example), show what you've tried so far, and explain exactly what's not working. Your question in it's current state is to broad, and not answerable. – Alicia Sykes Aug 06 '22 at 19:35
  • @Lissy93 Which would be the best approach? – TxFig Aug 06 '22 at 20:05
  • That depends on the requirements of what you're building :) If scores are user-specific, then local storage is the easiest. You can't host databases with GH pages, you'll need some kind of backend or serverless environment (like [CF-KV](https://www.cloudflare.com/en-gb/products/workers-kv/)). But gave several options in my previous comment ^^ – Alicia Sykes Aug 06 '22 at 20:27
  • 1
    See [upload files to github directory using github api](https://stackoverflow.com/q/38627115/438273) and [How to upload a file using GitHub API #24723](https://github.com/orgs/community/discussions/24723). – jsejcksn Aug 08 '22 at 10:50
  • Hey @TxFig have you found a way around your problem, cause I also have similar problem, want to update the html with javascript ! – Divinemonk Feb 14 '23 at 13:06

0 Answers0