initial commit
[anchor2post.git] / anchor2post.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 </head>
6 <body>
7 <!-- https://github.com/fpunktk/anchor2post -->
8 <script type="text/javascript">
9 var keyvals = document.URL.toString().split("#")[1]; // TODO: what happens if there is no # in the url?
10 var keyval_regex = /([^&=]+)=?([^&]*)/g;
11 var hist_length_regex = /([^ ]+)_history.length=(\d+)/g;
12 var tmp;
13 var input;
14 var anchorhash = "";
15 var hash_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
16 var action_set = "false";
17 var auto_submit = "true";
18 var anchorhash_found = "false";
19 var form = document.createElement("form");
20 form.setAttribute("method","post");
21
22 // calculate a hash from the anchor, not very unique or cryptographically secure, just transform two characters from keyvals to one character from hash_characters
23 for ( var i = 1; i < keyvals.length; i+=2 ) {
24     anchorhash += hash_characters.substr( (keyvals.charCodeAt(i-1) + keyvals.charCodeAt(i)) % hash_characters.length, 1 );
25 }
26
27 // process the anchor-string, extract the url, favicon and the data to post to the url
28 while ( tmp = keyval_regex.exec(keyvals) ) {
29     if ( decodeURIComponent(tmp[1]) == "anchor2posturl" ) {
30         form.setAttribute("action", decodeURIComponent(tmp[2]));
31         action_set = "true";
32     } else if ( decodeURIComponent(tmp[1]) == "anchor2postfavicon" ) {
33         var favicon = document.createElement("link");
34         favicon.setAttribute("rel", "icon");
35         favicon.setAttribute("href", decodeURIComponent(tmp[2]));
36         //favicon.setAttribute("type", "image/png"); // nobody knows the content type
37         document.getElementsByTagName("head")[0].appendChild(favicon);
38     } else {
39         input = document.createElement("input");
40         input.setAttribute("name", decodeURIComponent(tmp[1]));
41         input.setAttribute("value", decodeURIComponent(tmp[2]));
42         form.appendChild(input);
43     }
44 }
45
46 // set or read a cookie to determine whether to submit the form automatically or not; reason: if you go back to this site you most likely don't want to be automatically forwarded again
47 if ( navigator.cookieEnabled == true ) {
48     //alert("cookies allowed \n" + auto_submit + "\n" + document.cookie.replace(/ /g, "\n")); // debug
49     while ( tmp = hist_length_regex.exec(document.cookie) ) {
50         if ( tmp[1] == anchorhash ) {
51             // a cookie for this anchorhash is found, so it has already been submitted
52             // do not auto submit the form if the saved history length is different from the actual history length and from 1 (which means this is an new tab)
53             if ( history.length != 1 && history.length > tmp[2] ) {
54                 auto_submit = "false";
55             }
56             anchorhash_found = "true";
57             //alert(history.length + " " + tmp[2] + " " + auto_submit); // debug
58             break;
59         }
60     }
61     
62     // add this anchorhash to the cookie if it wasn't found
63     if ( anchorhash_found === "false" ) {
64         document.cookie = anchorhash + "_history.length=" + history.length;
65     }
66     
67     // update the cookies expiration time
68     // TODO: I think this is not working as expected, perhaps try document.cookie +=
69     var expiredate = new Date();
70     expiredate.setTime(expiredate.getTime() + (24 * 60 * 60 * 1000)); // 24 hours
71     document.cookie = "expires=" + expiredate.toGMTString();
72 } else {
73     // no cookie support, don't submit the form if there is a history
74     // TODO: better would be: if there is no forward history: submit the form; but I don't know how to find out whether there is a forward history without using cookies
75     if ( history.length != 1 ) {
76         auto_submit = "false";
77     }
78 }
79
80 if ( action_set === "true" ) {
81     if ( auto_submit === "true" ) {
82         form.setAttribute("style","display: none;");
83         document.body.appendChild(form);
84         form.submit();
85     } else {
86         // don't auto-submit the form, display a button
87         input = document.createElement("input");
88         input.setAttribute("type", "submit");
89         input.setAttribute("value", "submit form");
90         input.setAttribute("id", "id-form-submit-button");
91         form.appendChild(input);
92         document.body.appendChild(form);
93         document.getElementById("id-form-submit-button").focus();
94     }
95 } else {
96     document.write("<h1>anchor2posturl has not been set, no request was sent</h1>");
97 }
98 </script>
99
100 <noscript>
101 <h1>please enable javascript</h1>
102 <noscript>
103
104 </body>
105 </html>