Quine (JavaScript / URI)

From LiteratePrograms
Jump to: navigation, search
Other implementations: BASIC | Clojure | dc | Erlang | Forth | JavaScript / URI | Lisp | Oz | Perl | Python | Smalltalk

[edit] the data: URI quine

<<quine.uri>>=
data:text/html; 
base64,PGh0bWw+PGhlYWQ+Cjx0aXRsZT5kYXRhOiBVUkkgcXVpbmU8L3RpdGxlPgo8c2Nya 
XB0PjwhLS0KdmFyIHE9e246JzAnLGg6JyUzQ2h0bWwlM0UlM0NoZWFkJTNFJTBBJTNDdGl0b 
GUlM0VkYXRhOiUyMFVSSSUyMHF1aW5lJTNDL3RpdGxlJTNFJTBBJTNDc2NyaXB0JTNFJTNDJ 
TIxLS0lMEF2YXIlMjBxPSU3QicsdDonJTdEJTBBZnVuY3Rpb24lMjBpJTI4JTI5JTdCcSUyR 
W49K3ElMkVuKzE7ZG9jdW1lbnQlMkVnZXRFbGVtZW50QnlJZCUyOCUyMnglMjIlMjklMkVoc 
mVmKz1xJTJFaCtlbmNvZGVVUkklMjhmJTI4cSUyOSUyOStxJTJFdCU3RCUwQWZ1bmN0aW9uJ 
TIwZiUyOG8lMjklN0JsPSU1QiU1RDtmb3IlMjhwJTIwaW4lMjBvJTI5JTdCbCUyRXB1c2glM 
jhwKyUyMjolMjclMjIrbyU1QnAlNUQrJTIyJTI3JTIyJTI5JTdEcmV0dXJuJTIwbCUyRWpva 
W4lMjglMjIsJTIyJTI5JTdEJTBBLy8tLSUzRSUzQy9zY3JpcHQlM0UlM0MvaGVhZCUzRSUwQ 
SUzQ2JvZHklMjBvbkxvYWQ9JTIyaSUyOCUyOSUyMiUzRSUwQWNhcmUlMjB0byUyMHZpc2l0J 
TIwYSUyMCUyOHZlcnklMjBjbG9zZWx5JTIwcmVsYXRlZCUyOSUwQSUzQ2ElMjBpZD0lMjJ4J 
TIyJTIwaHJlZj0lMjJkYXRhOnRleHQvaHRtbCwlMjIlM0UlMEFkaWZmZXJlbnQlMjBwYWdlJ 
TNDL2ElM0U/ 
JTBBJTNDL2JvZHklM0UlM0MvaHRtbCUzRSd9CmZ1bmN0aW9uIGkoKXtxLm49K3EubisxO2Rv 
Y3VtZW50LmdldEVsZW1lbnRCeUlkKCJ4IikuaHJlZis9cS5oK2VuY29kZVVSSShmKHEpKStx 
LnR9CmZ1bmN0aW9uIGYobyl7bD1bXTtmb3IocCBpbiBvKXtsLnB1c2gocCsiOiciK29bcF0r 
IiciKX1yZXR1cm4gbC5qb2luKCIsIil9Ci8vLS0+PC9zY3JpcHQ+PC9oZWFkPgo8Ym9keSBv 
bkxvYWQ9ImkoKSI+CmNhcmUgdG8gdmlzaXQgYSAodmVyeSBjbG9zZWx5IHJlbGF0ZWQpCjxh 
IGlkPSJ4IiBocmVmPSJkYXRhOnRleHQvaHRtbCwiPgpkaWZmZXJlbnQgcGFnZTwvYT4/ 
CjwvYm9keT48L2h0bWw+

To "execute", copy the URI above, and paste it into your browser's location field.

[edit] the javascript

Maybe it is a little easier to see what is going on with the JavaScript side. It's like chickens and eggs. The data: URI produces a web page which links to a data: URI which produces a web page... So instead of saying that the data: URI reproduces itself when pasted into a browser, we could say that the javascript reproduces itself when the link is clicked.

Non-trivial quines are composed of at least a quotation, which contains the data necessary for reproduction, and a translation, which is the code that interprets the quotation. In this case, both of these are wrapped in a web page, suitable for reproducing in a browser.

<<quine.htm>>=
<html><head>
<title>data: URI quine</title>
<script><!--
quotation
translation
//--></script></head>
<body onLoad="i()">
care to visit a (very closely related)
<a id="x" href="data:text/html,">
different page</a>?
</body></html>

The translation is fairly straightforward. We reproduce everything before the quotation (quoted by q.h), the quotation itself, and then everything after (quoted by q.t). This function i() is called by the body onLoad, and appends the result to document.getElementById("x").href, thus initializing the link to the "different page".

  • the function f(), given a dictionary, reproduces its source. There is a method in recent javascripts to do this, but we explicitly code it here for the sake of compatibility.
  • we take advantage of the fact that the URI-encoding of a dictionary of strings is (for our keys) indistinguishable from the dictionary of an URI-encoded set of strings.
<<translation>>=
function i(){q.n=+q.n+1;document.getElementById("x").href+=q.h+encodeURI(f(q))+q.t}
function f(o){l=[];for(p in o){l.push(p+":'"+o[p]+"'")}return l.join(",")}

Finally, once one has the translation process worked out, the quotation is merely a duplication of the various parts of the page's entire source, quoted appropriately.

<<quotation>>=
var q={n:'0',h:'%3Chtml%3E%3Chead%3E%0A%3Ctitle%3Edata:%20URI%20quine%3C/title%3E%0A%3Cscript%3E%3C%21--%0Avar%20q=%7B',t:'%7D%0Afunction%20i%28%29%7Bq%2En=+q%2En+1;document%2EgetElementById%28%22x%22%29%2Ehref+=q%2Eh+encodeURI%28f%28q%29%29+q%2Et%7D%0Afunction%20f%28o%29%7Bl=%5B%5D;for%28p%20in%20o%29%7Bl%2Epush%28p+%22:%27%22+o%5Bp%5D+%22%27%22%29%7Dreturn%20l%2Ejoin%28%22,%22%29%7D%0A//--%3E%3C/script%3E%3C/head%3E%0A%3Cbody%20onLoad=%22i%28%29%22%3E%0Acare%20to%20visit%20a%20%28very%20closely%20related%29%0A%3Ca%20id=%22x%22%20href=%22data:text/html,%22%3E%0Adifferent%20page%3C/a%3E?%0A%3C/body%3E%3C/html%3E'}

[edit] some minor points

strictly speaking, this isn't a quine.

  • the data:URI given above is base64 encoded for convenience, but all subsequent generations of URI are consistently URL-encoded.
  • it does not reproduce itself exactly (unless you are willling to consider a "reproduction" over a very long cycle) Can you find the minor change that is made in each generation?
Download code
hijacker
hijacker
hijacker
hijacker