ip.flash.moe/script.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-06-24 00:27:57 +00:00
window.fwip = (function() {
this.ipv4s = document.querySelectorAll('[data-ipv="4"]');
this.ipv6s = document.querySelectorAll('[data-ipv="6"]');
this.doAddressLookup = function(version, callback) {
version = parseInt(version || 4).toString();
var url = '//ipv' + version + '.flash.moe/json';
fetch(url)
.then(resp => resp.json())
.then(data => callback(data))
.catch(err => callback({error: 'not available'}));
};
var lookupCallback = function(set) {
return function(result) {
for(var i = 0; i < set.length; ++i) {
if(!result.a)
set[i].classList.add('ip-lookup-failed');
else {
var copyTarget = set[i],
clickTarget = set[i].parentNode.parentNode;
clickTarget.onclick = function() { this.doAddressCopy(copyTarget) }.bind(this);
}
set[i].textContent = result.a || result.error || 'gone';
}
}.bind(this);
}.bind(this);
this.doAddressLookup(4, lookupCallback(this.ipv4s));
this.doAddressLookup(6, lookupCallback(this.ipv6s));
this.selectTextInElement = function(elem) {
// MSIE
if(document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(elem);
range.select();
return;
}
// Mozilla
if(window.getSelection) {
var select = window.getSelection(),
range = document.createRange();
range.selectNodeContents(elem);
select.removeAllRanges();
select.addRange(range);
return;
}
console.warn('Unable to select text.');
};
this.copySelectedText = function() {
if(document.execCommand) {
document.execCommand('copy');
return;
}
console.warn('Unable to copy text.');
};
this.selectNothing = function() {
// MSIE
if(document.body.createTextRange) {
document.body.createTextRange().select();
return;
}
// Mozilla
if(window.getSelection) {
window.getSelection().removeAllRanges();
return;
}
console.warn('Unable to select text.');
};
this.doAddressCopy = function(elem) {
this.selectTextInElement(elem);
this.copySelectedText();
//this.selectNothing();
};
return this;
}).call(window.fwip || {});