Skip to content

Commit

Permalink
Move JavaScript snippet to separate file (#83)
Browse files Browse the repository at this point in the history
This allows to use a more restrictive CSP (especially without unsafe-inline script).

See https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
  • Loading branch information
xi authored and patrickrobrecht committed May 19, 2018
1 parent 670c25c commit df6f003
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 8 additions & 0 deletions js/snippet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const statifyReq = new XMLHttpRequest();
statifyReq.open(
'GET',
document.getElementById('statify-js-snippet').getAttribute('data-home-url')
+ '?statify_referrer=' + encodeURIComponent(document.referrer)
+ '&statify_target=' + encodeURIComponent(location.pathname + location.search)
);
statifyReq.send( null );
14 changes: 5 additions & 9 deletions views/js_snippet.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
class_exists( 'Statify' ) || exit; ?>

<!-- Stats by http://statify.de -->
<script type="text/javascript">
const statifyReq = new XMLHttpRequest();
statifyReq.open(
'GET',
'<?php echo esc_url( home_url( '/', 'relative' ) ); ?>'
+ '?statify_referrer=' + encodeURIComponent(document.referrer)
+ '&statify_target=' + encodeURIComponent(location.pathname + location.search)
);
statifyReq.send( null );
<script
id="statify-js-snippet"
data-home-url="<?php echo esc_url( home_url( '/', 'relative' ) ); ?>"
type="text/javascript"
src="<?php echo plugins_url( 'js/snippet.js', STATIFY_FILE ); ?>">
</script>

<?php /** Markup space */ ?>

0 comments on commit df6f003

Please sign in to comment.