2015-11-13 19:10:23 +00:00
|
|
|
// store code in the URL
|
|
|
|
window.common = (function(global) {
|
|
|
|
const {
|
|
|
|
encodeURIComponent: encode,
|
|
|
|
decodeURIComponent: decode,
|
|
|
|
location,
|
|
|
|
history,
|
|
|
|
common = { init: [] }
|
|
|
|
} = global;
|
|
|
|
|
|
|
|
const {
|
|
|
|
replaceScriptTags,
|
|
|
|
replaceSafeTags,
|
|
|
|
replaceFormActionAttr,
|
|
|
|
replaceFccfaaAttr
|
|
|
|
} = common;
|
|
|
|
|
2015-12-05 22:53:33 +00:00
|
|
|
const queryRegex = /^(\?|#\?)/;
|
2015-11-13 19:10:23 +00:00
|
|
|
function encodeFcc(val) {
|
|
|
|
return replaceScriptTags(replaceFormActionAttr(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeFcc(val) {
|
|
|
|
return replaceSafeTags(replaceFccfaaAttr(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
var codeUri = {
|
|
|
|
encode: function(code) {
|
|
|
|
return encode(code);
|
|
|
|
},
|
|
|
|
decode: function(code) {
|
|
|
|
try {
|
|
|
|
return decode(code);
|
|
|
|
} catch (ignore) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isInQuery: function(query) {
|
|
|
|
var decoded = codeUri.decode(query);
|
|
|
|
if (!decoded || typeof decoded.split !== 'function') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decoded
|
2015-12-05 22:53:33 +00:00
|
|
|
.replace(queryRegex, '')
|
2015-12-03 22:07:39 +00:00
|
|
|
.split('&')
|
2015-11-13 19:10:23 +00:00
|
|
|
.reduce(function(found, param) {
|
|
|
|
var key = param.split('=')[0];
|
|
|
|
if (key === 'solution') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}, false);
|
|
|
|
},
|
|
|
|
isAlive: function() {
|
|
|
|
return codeUri.enabled &&
|
|
|
|
codeUri.isInQuery(location.search) ||
|
|
|
|
codeUri.isInQuery(location.hash);
|
|
|
|
},
|
2015-12-03 22:07:39 +00:00
|
|
|
getKeyInQuery(query, keyToFind = '') {
|
|
|
|
return query
|
|
|
|
.split('&')
|
|
|
|
.reduce(function(oldValue, param) {
|
|
|
|
var key = param.split('=')[0];
|
2015-12-05 22:53:33 +00:00
|
|
|
var value = param
|
|
|
|
.split('=')
|
|
|
|
.slice(1)
|
|
|
|
.join('=');
|
|
|
|
|
2015-12-03 22:07:39 +00:00
|
|
|
if (key === keyToFind) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return oldValue;
|
|
|
|
}, null);
|
|
|
|
},
|
|
|
|
getSolutionFromQuery(query = '') {
|
|
|
|
return decodeFcc(
|
|
|
|
codeUri.decode(codeUri.getKeyInQuery(query, 'solution'))
|
|
|
|
);
|
|
|
|
},
|
2015-11-13 19:10:23 +00:00
|
|
|
parse: function() {
|
|
|
|
if (!codeUri.enabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var query;
|
|
|
|
if (location.search && codeUri.isInQuery(location.search)) {
|
|
|
|
query = location.search.replace(/^\?/, '');
|
2015-12-03 22:07:39 +00:00
|
|
|
|
2015-11-13 19:10:23 +00:00
|
|
|
if (history && typeof history.replaceState === 'function') {
|
|
|
|
history.replaceState(
|
|
|
|
history.state,
|
|
|
|
null,
|
|
|
|
location.href.split('?')[0]
|
|
|
|
);
|
|
|
|
location.hash = '#?' + encodeFcc(query);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = location.hash.replace(/^\#\?/, '');
|
|
|
|
}
|
2015-12-03 22:07:39 +00:00
|
|
|
|
2015-11-13 19:10:23 +00:00
|
|
|
if (!query) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-12-03 22:07:39 +00:00
|
|
|
return this.getSolutionFromQuery(query);
|
2015-11-13 19:10:23 +00:00
|
|
|
},
|
|
|
|
querify: function(solution) {
|
|
|
|
if (!codeUri.enabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (history && typeof history.replaceState === 'function') {
|
2015-12-04 01:37:00 +00:00
|
|
|
// grab the url up to the query
|
|
|
|
// destroy any hash symbols still clinging to life
|
|
|
|
const url = (location.href.split('?')[0]).replace(/(#*)$/, '');
|
2015-11-13 19:10:23 +00:00
|
|
|
history.replaceState(
|
|
|
|
history.state,
|
|
|
|
null,
|
2015-12-04 01:37:00 +00:00
|
|
|
url +
|
|
|
|
'#?' +
|
|
|
|
(codeUri.shouldRun() ? '' : 'run=disabled&') +
|
|
|
|
'solution=' +
|
|
|
|
codeUri.encode(encodeFcc(solution))
|
2015-11-13 19:10:23 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
location.hash = '?solution=' +
|
|
|
|
codeUri.encode(encodeFcc(solution));
|
|
|
|
}
|
|
|
|
|
|
|
|
return solution;
|
|
|
|
},
|
2015-12-03 22:07:39 +00:00
|
|
|
enabled: true,
|
|
|
|
shouldRun() {
|
|
|
|
return !this.getKeyInQuery(
|
2015-12-05 22:53:33 +00:00
|
|
|
(location.search || location.hash).replace(queryRegex, ''),
|
2015-12-03 22:07:39 +00:00
|
|
|
'run'
|
|
|
|
);
|
|
|
|
}
|
2015-11-13 19:10:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
common.init.push(function() {
|
|
|
|
codeUri.parse();
|
|
|
|
});
|
|
|
|
|
|
|
|
common.codeUri = codeUri;
|
2015-12-03 23:09:52 +00:00
|
|
|
common.shouldRun = () => codeUri.shouldRun();
|
2015-11-13 19:10:23 +00:00
|
|
|
|
|
|
|
return common;
|
|
|
|
}(window));
|