Warns on licking links and arriving into sites which uses PunyCode.
目前為
// ==UserScript==
// @name PunyCode Protection
// @namespace PunyCode Protection
// @version 1.0.1
// @description Warns on licking links and arriving into sites which uses PunyCode.
// @author jcunews
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
//punycode source: https://github.com/bestiejs/punycode.js/blob/master/punycode.js
//comments are stripped.
punycode = (function() {
const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
const base = 36;
const tMin = 1;
const tMax = 26;
const skew = 38;
const damp = 700;
const initialBias = 72;
const initialN = 128; // 0x80
const delimiter = '-'; // '\x2D'
const regexPunycode = /^xn--/;
const regexNonASCII = /[^\0-\x7E]/; // non-ASCII chars
const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
const errors = {
'overflow': 'Overflow: input needs wider integers to process',
'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
'invalid-input': 'Invalid input'
};
const baseMinusTMin = base - tMin;
const floor = Math.floor;
const stringFromCharCode = String.fromCharCode;
/*--------------------------------------------------------------------------*/
function error(type) {
throw new RangeError(errors[type]);
}
function map(array, fn) {
const result = [];
let length = array.length;
while (length--) {
result[length] = fn(array[length]);
}
return result;
}
function mapDomain(string, fn) {
const parts = string.split('@');
let result = '';
if (parts.length > 1) {
result = parts[0] + '@';
string = parts[1];
}
string = string.replace(regexSeparators, '\x2E');
const labels = string.split('.');
const encoded = map(labels, fn).join('.');
return result + encoded;
}
function ucs2decode(string) {
const output = [];
let counter = 0;
const length = string.length;
while (counter < length) {
const value = string.charCodeAt(counter++);
if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
const extra = string.charCodeAt(counter++);
if ((extra & 0xFC00) == 0xDC00) { // Low surrogate.
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
} else {
output.push(value);
counter--;
}
} else {
output.push(value);
}
}
return output;
}
const ucs2encode = array => String.fromCodePoint(...array);
const basicToDigit = function(codePoint) {
if (codePoint - 0x30 < 0x0A) {
return codePoint - 0x16;
}
if (codePoint - 0x41 < 0x1A) {
return codePoint - 0x41;
}
if (codePoint - 0x61 < 0x1A) {
return codePoint - 0x61;
}
return base;
};
const digitToBasic = function(digit, flag) {
return digit + 22 + 75 * (digit < 26) - ((flag !== 0) << 5);
};
const adapt = function(delta, numPoints, firstTime) {
let k = 0;
delta = firstTime ? floor(delta / damp) : delta >> 1;
delta += floor(delta / numPoints);
for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {
delta = floor(delta / baseMinusTMin);
}
return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
};
const decode = function(input) {
const output = [];
const inputLength = input.length;
let i = 0;
let n = initialN;
let bias = initialBias;
let basic = input.lastIndexOf(delimiter);
if (basic < 0) {
basic = 0;
}
for (let j = 0; j < basic; ++j) {
if (input.charCodeAt(j) >= 0x80) {
error('not-basic');
}
output.push(input.charCodeAt(j));
}
for (let index = basic > 0 ? basic + 1 : 0; index < inputLength;) {
let oldi = i;
for (let w = 1, k = base; /* no condition */; k += base) {
if (index >= inputLength) {
error('invalid-input');
}
const digit = basicToDigit(input.charCodeAt(index++));
if (digit >= base || digit > floor((maxInt - i) / w)) {
error('overflow');
}
i += digit * w;
const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
if (digit < t) {
break;
}
const baseMinusT = base - t;
if (w > floor(maxInt / baseMinusT)) {
error('overflow');
}
w *= baseMinusT;
}
const out = output.length + 1;
bias = adapt(i - oldi, out, oldi === 0);
if (floor(i / out) > maxInt - n) {
error('overflow');
}
n += floor(i / out);
i %= out;
output.splice(i++, 0, n);
}
return String.fromCodePoint(...output);
};
const encode = function(input) {
const output = [];
input = ucs2decode(input);
let inputLength = input.length;
let n = initialN;
let delta = 0;
let bias = initialBias;
for (const currentValue of input) {
if (currentValue < 0x80) {
output.push(stringFromCharCode(currentValue));
}
}
let basicLength = output.length;
let handledCPCount = basicLength;
if (basicLength) {
output.push(delimiter);
}
while (handledCPCount < inputLength) {
let m = maxInt;
for (const currentValue of input) {
if (currentValue >= n && currentValue < m) {
m = currentValue;
}
}
const handledCPCountPlusOne = handledCPCount + 1;
if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
error('overflow');
}
delta += (m - n) * handledCPCountPlusOne;
n = m;
for (const currentValue of input) {
if (currentValue < n && ++delta > maxInt) {
error('overflow');
}
if (currentValue == n) {
let q = delta;
for (let k = base; ; k += base) {
const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
if (q < t) {
break;
}
const qMinusT = q - t;
const baseMinusT = base - t;
output.push(
stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
);
q = floor(qMinusT / baseMinusT);
}
output.push(stringFromCharCode(digitToBasic(q, 0)));
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
delta = 0;
++handledCPCount;
}
}
++delta;
++n;
}
return output.join('');
};
const toUnicode = function(input) {
return mapDomain(input, function(string) {
return regexPunycode.test(string) ? decode(string.slice(4).toLowerCase()) : string;
});
};
const toASCII = function(input) {
return mapDomain(input, function(string) {
return regexNonASCII.test(string) ? 'xn--' + encode(string) : string;
});
};
/*--------------------------------------------------------------------------*/
return {
'version': '2.1.0',
'ucs2': {
'decode': ucs2decode,
'encode': ucs2encode
},
'decode': decode,
'encode': encode,
'toASCII': toASCII,
'toUnicode': toUnicode
};
})();
//punycode end
function getEncoded(s) {
var hn = location.hostname.split("."), i, z;
for (i = 0; i < hn.length; i++) {
var match = hn[i].match(/^xn--(.*)$/);
if (match) {
try {
hn[i] = punycode.decode(match[1]);
} catch(z) {}
}
}
return hn.join(".");
}
//warn upon arriving into a possibly fake sites
var hn = getEncoded(location.hostname);
if (hn !== location.hostname) {
if (confirm("Warning! This website real domain name is:\n\n" + location.hostname + "\n\nDo you want to close this tab?")) {
var ele = document.createElement("SCRIPT");
ele.text = "window.close()";
document.head.appendChild(ele);
}
}
//warn on clicking a link pointing to a possibly fake site
addEventListener("click", function(ev) {
if (ev.target && (ev.target.tagName === "A")) {
var hn = getEncoded(ev.target.hostname);
if (hn !== ev.target.hostname) {
if (!confirm("Warning! About to go to a web page whose real domain name is:\n\n" + ev.target.hostname)) {
ev.preventDefault();
if (ev.stopPropagation) ev.stopPropagation();
if (ev.stopImmediatePropagation) ev.stopImmediatePropagation();
return false;
}
}
}
return true;
}, true);