Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modify snippet links with extra parameters.

  • Loading branch information...
commit a4f73aaa17694c1900d8acdabed2fbaf220729ca 1 parent 334bd19
Michael Kelly Osmose authored
Showing with 69 additions and 40 deletions.
  1. +0 −40 standard-js/script.js
  2. +69 −0 standard-js/snippet.html
40 standard-js/script.js
View
@@ -1,40 +0,0 @@
-//<![CDATA[
-var snippet_container = document.getElementById('snippetContainer');
-var snippets = snippet_container.getElementsByClassName('snippet');
-if (snippets.length > 0) {
- var show_snippet = snippets[Math.floor(Math.random()*snippets.length)];
- show_snippet.style.display = 'block';
- try {
- activateSnippetsButtonClick(show_snippet);
- } catch (err) {
- // Do nothing, most likely a newer version w/o
- // activateSnippetsButtonClick
- }
-
- // Send impression to the snippets stats server.
- var snippet_id = show_snippet.parentNode.dataset.snippetId;
- send_impression(snippet_id);
-
- // Trigger show_snippet event on snippet node.
- var evt = document.createEvent('Event');
- evt.initEvent('show_snippet', true, true);
- show_snippet.dispatchEvent(evt);
-} else {
- localStorage['snippets'] = '';
- showSnippets();
-}
-
-// Notifies stats server that the given snippet ID
-// was shown. No personally-identifiable information
-// is sent.
-function send_impression(id) {
- var sample_rate = 0.1;
- var url = 'https://snippets-stats.mozilla.org/foo.html';
-
- if (Math.random() <= sample_rate) {
- var r = XMLHttpRequest();
- r.open('GET', url + '?snippet_name=' + id);
- r.send();
- }
-}
-//]]>
69 standard-js/snippet.html
View
@@ -0,0 +1,69 @@
+<script type="text/javascript">
+//<![CDATA[
+(function() {
+
+ var SAMPLE_RATE = 0.1;
+ var STATS_URL = 'https://snippets-stats.mozilla.org/foo.html';
+
+ var snippets = (document.getElementById('snippetContainer')
+ .getElementsByClassName('snippet'));
+ if (snippets.length > 0) {
+ var show_snippet = snippets[Math.floor(Math.random() * snippets.length)];
+ show_snippet.style.display = 'block';
+
+ try {
+ activateSnippetsButtonClick(show_snippet);
+ } catch (err) {
+ // Do nothing, most likely a newer version of Firefox w/o
+ // activateSnippetsButtonClick
+ }
+
+ // Send impression to the snippets stats server.
+ var show_snippet_id = show_snippet.parentNode.dataset.snippetId;
+ send_impression(show_snippet_id);
+
+ // Trigger show_snippet event on snippet node.
+ var evt = document.createEvent('Event');
+ evt.initEvent('show_snippet', true, true);
+ show_snippet.dispatchEvent(evt);
+
+ // Add sample rate and snippet ID to currently displayed links.
+ var parameters = ('sample_rate=' + SAMPLE_RATE + '&snippet_id=' +
+ show_snippet_id);
+ modifyLinks(show_snippet.getElementsByTagName('a'), parameters);
+ } else {
+ localStorage['snippets'] = '';
+ showSnippets();
+ }
+
+ // Notifies stats server that the given snippet ID
+ // was shown. No personally-identifiable information
+ // is sent.
+ function send_impression(id) {
+ if (Math.random() <= SAMPLE_RATE) {
+ var r = new XMLHttpRequest();
+ r.open('GET', STATS_URL + '?snippet_name=' + id);
+ r.send();
+ }
+ }
+
+ // Modifies the given links to include the specified GET parameters.
+ function modifyLinks(links, parameters) {
+ for (var k = 0, len = links.length; k < len; k++) {
+ var link = links[k];
+ var delimeter = (link.href.indexOf('?') !== -1 ? '&' : '?');
+
+ // Pull the fragment off of the link
+ var fragment_position = link.href.indexOf('#');
+ if (fragment_position === -1) fragment_position = link.href.length;
+
+ var href = link.href.substring(0, fragment_position);
+ var fragment = link.href.substring(fragment_position);
+
+ link.href = href + delimeter + parameters + fragment;
+ }
+ }
+
+})();
+//]]>
+</script>
Please sign in to comment.
Something went wrong with that request. Please try again.