r/javascript • • Sep 28 '24

AskJS [AskJS] Internal JS in a local HTML on Safari on Iphone

Safari on Iphone 13 doesn't run internal JavaScript of a local HTML file. The Iphone has no option to open the file with Chrome. Chrome has not a menu option to open a local file. Ctrl+o depends on a physical keyboard. The file is proprietary, so it cannot be served from the Internet. How to run the internal Javascript of a local HTML file on an Iphone?

4 Upvotes

11 comments sorted by

1

u/ethanjf99 Sep 28 '24

you mean something just in Files on your phone?

3

u/shgysk8zer0 Sep 28 '24

What do you mean by "local"? file:, http://localhost...?

If it's file:, I thought all browsers had fixed that huge risk long ago.

You could always use a data: URI if nothing else. Ideally URL encoded rather than base64.

2

u/PeaWilling6069 Jan 26 '25

One option on iOS is to get “Koder” from the iOS App Store, which allows you to import html source files into a virtual environment that allows multi-file interaction. I loaded an html file that referenced an external JavaScript file and external CSS file, and the virtual environment enabled those references to work, and Koder’s internal browser rendered the webpage correctly. (The convenience of Koder avoids having to merge js/css into a unified file, and avoids the need to build a web server so that Chrome or Safari can find the files!)

1

u/ethanjf99 Sep 28 '24

may not be doable for security purposes. i think Apple may have shut off that API. you can try mS Edge

1

u/TemporaryTemp100 Sep 28 '24

there are two workarounds and both solutions are required for a ios shortcut triggering and copying content of local html file to shortcuts as clipboard or text:

1- download scriptable from Appstore and use inline script to run your local html file with javascript code (copy content as text)

2- base encode your html file, use this pattern in url: data:text/html;base64,Base64 Encoded

get contents of the file and shownin WebView.

I hope this is what you're looking for.

0

u/guest271314 Sep 28 '24

You should be able to use HTML <input type="file"> to get the local file as a File object.

1

u/Pedro41RJ Sep 28 '24

It is a game written in HTML, CSS and JavaScript. The JavaScript must run for the user play the game.

1

u/guest271314 Sep 28 '24

Yes. Use HTML <input type="file"> to get the local file as a File object, then use URL.createObjectURL() to create a Blob URL from the File, then run your code.

All modern browsers support W3C File API.

E.g.,

document.querySelector("input[type=file]")
.onchange = (e) => {
  const file = e.target.files[0];
  const blobURL = URL.createObjectURL(file);
  const script = document.createElement("script");
  script.src = blobURL;
  document.body.appendChild(script);
}

2

u/Pedro41RJ Sep 28 '24

My software contains HTML and inline CSS, too. It is not just JavaScript. It is a full html with internal JavaScript.

1

u/Pedro41RJ Sep 28 '24

Your code is JavaScript, which is not run locally. So, it must have an online component and a local component. It is too complex for most users.