-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterp.js
More file actions
100 lines (99 loc) · 3.01 KB
/
interp.js
File metadata and controls
100 lines (99 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$ (document).ready (load);
function load (){
$ ("#btn-run").click (run);
$ ("#stop").click(function(){
detour.stop = true;
});
$ ("#interval").change(function (){
detour.interval = this.value;
$("#show-interval").text(this.value);
});
setInterval(function(){
$("#bytes").text(" - " + $("#source").val().length + " bytes");
});
$("#permalink").click(function(){
window.open(applyquery({hex:btoa($("#source").val())}, 'https://rawgit.com/cyoce/detour/master/interp.html'));
});
$("#markdown").click(function(){
var source = $("#source").val();
var out = "# [Detour](https://rawgit.com/cyoce/detour/master/interp.html), ";
out += source.length + " bytes\n";
out += ("\n" + source).replace(/\n/g, "\n ");
out += "\n\n[Try it online!](" + applyquery({hex:btoa($('#source').val())}, 'https://rawgit.com/cyoce/detour/master/interp.html') + ")";
$("#source").val(out).select();
document.execCommand("copy");
$("#source").val(source);
})
var query = parse_query(location.href);
if (query) {
if (query.hex){
$("#source").val(atob(query.hex));
} else {
$("#source").val(query.code);
}
}
if (query === null) query = {};
function applyquery(query, href) {
href = href || location.href;
href = href.split("?")[0];
return href + gen_query(query);
}
function parse_query(href) {
href = String(href)
.split("?");
if (href.length <= 1) return null;
href = href[1];
var out = {};
var keys = href.split("&");
for (var i = 0; i < keys.length; i++) {
var
pair = keys[i].split('=');
out[unescape(pair[0])] = unescape(pair[1]);
}
return out;
}
function gen_query(obj) {
if (obj === null || obj === Object.create(null)) return '';
var out = '?';
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if (out.length !== 1) out += "&";
out += escape(key) + "=" + escape(obj[key]);
}
return out;
}
function base64(value){
var digits = "0123456789abcdeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+";
if (typeof value === "string"){
return digits.indexOf(value [1]) + 64 * digits.indexOf(value [0]);
} else {
return digits [Math.floor(value/64)] + digits [value % 64];
}
}
function hexdump (string){
return [...string].map(x => x.charCodeAt().toString(16)).map (x=>"0".repeat(2-x.length) + x).join('');
}
function hexcompress(string){
var hex = hexdump(string);
hex = "0".repeat(3-hex.length%3) + hex;
var matches = hex.match(/.../g);
if (matches[0] === "000") matches.splice(0,1);
var out = [];
for (var i = 0; i < matches.length; i++){
var match = matches[i];
var int = parseInt(match, 16);
var num = base64(int);
out.push(num)
}
return out.join('');
}
function hexdecompress(string){
string = "0".repeat(string.length%2) + string;
var matches = string.match(/../g), out='';
for (var i = 0;i < matches.length; i++){
var num = base64(matches[i]).toString(16);
out += num;
}
return unescape(out.replace(/../g, "%$&"));
}
}