parsimonhi / maxiGos

Game of Go Web Viewer and sgf editing program
8 stars 0 forks source link

Unable to load URLs with Parameters #2

Closed Nik-EWH closed 10 months ago

Nik-EWH commented 1 year ago

Hi,

It seems that maxiGos does not work when the data-maxigos-sgf javascript attribute contains a URL with a query/parameter (e.g. www.example.com/endgame.sgf?skip_redirect=true). Is there a way to fix this?

More context: I am trying to use maxiGos to load a SGF viewer on a website any time a visitor clicks a link to an sgf file on the site.

I created a .htaccess RewriteRule to point any .sgf file to a dedicated sgf-viewer.php page template with an appended ?sgf_file=x parameter. For example, https://www.example.com/endgame.sgf will redirect to https://www.example.com/sgf-viewer/?sgf_file=https://www.example.com/endgame.sgf

The sgf-viewer.php page contains:

<script src="https://example.com/maxigos/maxigos-eidogo-tree.js" data-maxigos-sgf="PLACEHOLDER_SGF_URL"></script>
    <script>
        // Replace the placeholder SGF URL with the actual URL
        var sgfFile = '<?php echo esc_js($_GET['sgf_file']); ?>';
        // Append the skip_redirect parameter
        sgfFile += '&skip_redirect=true';
        var scriptTag = document.querySelector('[data-maxigos-sgf="PLACEHOLDER_SGF_URL"]');
        scriptTag.dataset.maxigosSgf = sgfFile;
    </script>

I added the '&skip_redirect=true' parameter because without it the .htaccess redirect would act again and rewrite the requested URL also. Then I updated my .htaccess rule with a !skip_redirect=true condition.

But, this still does not work, and when I tested it with a hardcoded example and noticed that even with the rewriterules disabled, data-maxigos-sgf="https://example.com/endgame.sgf" works but data-maxigos-sgf="https://example.com/endgame.sgf?x=y" does not.

parsimonhi commented 10 months ago

Hi,

Sorry to be so late.

It's possible to fix your problem. By default, maxiGos expects that the file name ends by the string ".sgf". You can modify it using the sourceFilter parameter of maxiGos (or data-maxigos-source-filter as a script tag attribute). Its value is a regex that allows maxiGos to check if the sgf file name has a convenient format. Its default value is "^[^?]+\.sgf$". If you use a regex such as "^.*$", it will work for any file name, but a more restrictive regex can be used too (more it is restrictive, more it is secure).

For instance:

Nik-EWH commented 10 months ago

Thank you! That worked, fantastic! Not sure if I missed it but if this is not in the documentation already I suggest adding it. Very happy my project can proceed now!