1

I have a Java application that as part of it's output creates an html report ,then opens the html reports in users browser. On Firefox and Google Chrome this works but on Safari it opens the report as if Javascript was not enabled, even though it is. However if you reopen the report by clicking on a link from another webpage (which lists all reports) then it opens fine in Safari.

What do I need to do to trigger Safari to open the report with Javascript enabled.

Console shows some errors, but I dont understand them

enter image description here

This is related issue https://apple.stackexchange.com/questions/361245/safari-kcferrordomaincfnetwork-error-1-on-local-html-files but doesn't provide a satisafactory answer.

Actually the answer here https://apple.stackexchange.com/questions/366448/safari-giving-kcferrordomaincfnetwork-error-303-when-visiting-a-site about removing my site from Preferecnes:Privacy works but that is no good because the problem occurs on new computer after running the program only a few times so would have to continually do it.

2
  • Do you have any error in the debug console of Safari ? Commented Feb 10, 2021 at 14:54
  • thx yes they do, added screenshot Commented Feb 10, 2021 at 19:02

2 Answers 2

1

You open the file locally. Browsers usually restrict local file processing for security reasons. You must "Disable local file restrictions" in Safari in the "Developer" menu for making this possible. I am not sure, but it might be necessary to do this each time Safari opens such a file via your application. Embedding all external resources might also help, but I am not sure.

Opening the remote URL should always work. So this would be the best option. As an alternative you could serve the file to the browser via an embedded HTTP server in your application.

Sign up to request clarification or add additional context in comments.

8 Comments

'Browsers usually restrict local file processing for security reasons', why is a local file more insecure than a remote file, problem only occurs on Safari, okay on Firefox and Chrome
Also a detail I think yo have missed is that if open the page from another local webpage then there is no problem.
Yeah, since I do not use Safari this was more a guessing game. Sorry! I think this blog post describes your problem: eclecticlight.co/2018/10/18/… My proposed solutions that do not depend on the browser still stand.
Regarding external resources in local resources, there usually should be a security policy in place: stackoverflow.com/questions/20748630/…
'Opening the remote URL should always work', so is there a remote url version of the local file url that i can use ?
|
1
+200

Edit: i just saw the previous answer proposed the same workaround, but i think copy paste code is always delicious. As for your problem, i verified this is occuring on safari and nothing else, and for me this fix worked.

Edit 2: According to this answer, it is a bug in Safari that it works when you open it via hyperlink. Without changing the developer settings in every users browser, it won't be working except on a local server. I'm sorry but it seems there is no other in-code workaround except what i already provided.

If you want to open with scripts at any cost, you can to the following workaround:

  • Write a minimalistic server socket (or copy paste my code):
    Index.java:
package index;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Index extends Base {
    public byte[] buildResponseBody(String resource) {
        try {
            System.out.println(resource);
            if(resource.contains("?"))  return "<!DOCTYPE HTML><html><h1>Error 404: Page not found</h1></html>".getBytes(StandardCharsets.US_ASCII);
            return Files.readAllBytes(Paths.get(resource));
        } catch (Exception e) {
            return "<!DOCTYPE HTML><html><h1>Error 404: Page not found</h1></html>".getBytes(StandardCharsets.US_ASCII);
        }
    }

    public static void main(String[] args) throws IOException {
        new Index().loop(80);
    }
}

Base.java:

package index;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Base {
    public abstract byte[] buildResponseBody(String resource);

    String receiveRequest(BufferedReader reader) throws IOException {
        final Pattern getLinePattern = Pattern.compile("(?i)GET\\s+/(.*?)\\s+HTTP/1\\.[01]");
        String resource = null;
        try {
            for (String line = reader.readLine(); !line.isEmpty(); line = reader.readLine()) {
                Matcher matcher = getLinePattern.matcher(line);
                if (matcher.matches()) {
                    resource = matcher.group(1);
                }
            }
        } catch (NullPointerException e) {
            return null;
        }
        return resource;
}

    public void sendResponse(String resource, OutputStream output, PrintWriter writer) throws IOException {
        byte[] responseBody = buildResponseBody(resource);
        if (responseBody == null) {
            writer.println("HTTP/1.0 404 Not found");
            responseBody = "Not found".getBytes();
        } else
            writer.println("HTTP/1.0 200 OK");
        writer.println("Server: " + getClass().getSimpleName());
        writer.println("Content-Length: " + responseBody.length);
        writer.println();
        writer.flush();
        output.write(responseBody);
    }

    public void loop(int port) throws IOException {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            while (true)
                try (Socket socket = serverSocket.accept();
                     InputStream input = socket.getInputStream();
                     BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                     OutputStream output = socket.getOutputStream();
                     PrintWriter writer = new PrintWriter(new OutputStreamWriter(output))) {
                    sendResponse(receiveRequest(reader), output, writer);
                }
        }
    }
}

This enables opening a page at 127.0.0.1:80. If you now make sure the document is accessable to your application, you can make it open a browser window at your localhost:80/file.html. Since it now runs over an actual website not just a file in safari, the scripts should theoretically be working. (At least mine are in this code) You might want to improve security and remove some bugs of this code. I hope i could help.

2 Comments

This is helpful, but is this a bug in Safari and why can a html page that lists rpeorts and open the report simply as <a href="FixSongsReport00155/FixSongsReport00155.html"> work, since this is also local file. If you would like to test actual program it can be downloaded and run fro free at jthink.net/songkong/en/download.jsp
I am going to take a look on it, but i have absolutely no idea why this is happening. I also think it is more of a bug in Safari. Another workaround might be to open the list document in a way that the link is always at the top and then use java.awt.Robot to simulate a click on the link. Should be easier to implement but requeres the page to load fast (or you add delay)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.