ip.flash.moe/public/script.js

102 lines
3 KiB
JavaScript

var createXHR = function() {
if('all' in document && !('atob' in window) && 'XDomainRequest' in window)
return new XDomainRequest;
if('XMLHttpRequest' in window)
return new XMLHttpRequest;
if('ActiveXObject' in window)
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {}
}
throw 'no impl';
}
var getRemoteString = function(url, callback) {
try {
var xhr = createXHR();
xhr.onload = function(ev) {
callback({ success: true, info: ev, text: xhr.responseText });
};
xhr.onerror = function(ev) {
callback({ success: false, info: ev });
};
xhr.open('GET', url);
xhr.send();
} catch(ex) {
callback({ success: false, info: ex });
}
};
var copyTextInElement = function(target, skipClipboardAPI) {
if(!skipClipboardAPI && 'clipboard' in navigator) {
// for some reason I'm supporting IE8 with this and it gets mad over the catch keyword...
navigator.clipboard.writeText(target.textContent)['catch'](function(ex) {
copyTextInElement(target, true);
});
return;
}
if('execCommand' in document) {
if('createTextRange' in document.body) { // MSIE
var range = document.body.createTextRange();
range.moveToElementText(target);
range.select();
} else if('getSelection' in window) { // Mozilla
var select = window.getSelection();
var range = document.createRange();
range.selectNodeContents(target);
select.removeAllRanges();
select.addRange(range);
} else return;
document.execCommand('copy');
return;
}
};
(function() {
var fields = document.querySelectorAll('.js-address');
for(var i = 0; i < fields.length; ++i)
(function(field) {
var valueField = field.querySelector('.js-address-value');
if(valueField === null)
return;
var setValueField = function(text) {
valueField['textContent' in valueField ? 'textContent' : 'innerText'] = text;
};
setValueField('loading...');
var subdomain = field.getAttribute('data-subdomain');
if(typeof subdomain !== 'string')
return;
var copyButton = field.querySelector('.js-address-copy');
var host = location.host.split('.');
host[0] = subdomain;
var url = location.protocol + '//' + host.join('.') + '/';
getRemoteString(url, function(info) {
if(!info.success) {
setValueField('not available');
return;
}
setValueField(info.text);
if(copyButton)
copyButton.onclick = function() { copyTextInElement(valueField); };
});
})(fields[i]);
})();