<commit_before>var notification = false,
	notificationAvid = {},
	playerTabs = {},
	cidHackType = {},
	viCache = {},
	locale = 0,
	localeAcquired = false,
	localeTimeout = null;

URL.prototype.__defineGetter__('query', function() {
	var parsed = this.search.substr(1).split('&');
	var parsedObj = {};
	parsed.forEach(function(elem, iter, arr) {
		var vals = arr[iter].split('=');
		parsedObj[vals[0]] = vals[1];
	});
	return parsedObj;
});

var randomIP = function(fakeip) {
	var ip_addr = '220.181.111.';
	if (fakeip == 2) ip_addr = '59.152.193.';
	ip_addr += Math.floor(Math.random() * 254 + 1);
	return ip_addr;
}

function getFileData(url, callback, fakeip) {
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url, true);
	if (fakeip && locale != fakeip) {
		var ip = randomIP(fakeip);
		xmlhttp.setRequestHeader('Client-IP', ip);
		xmlhttp.setRequestHeader('X-Forwarded-For', ip);
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (typeof callback == "function") callback(xmlhttp.responseText);
		} else if (xmlhttp.status > 400) {
			if (typeof callback == "function") callback("{}");
		}
	}
	xmlhttp.send();
}

function postFileData(url, data, callback) {
	var encodeData = "",
		append = false;
	Object.keys(data).forEach(function(key) {
		if (!append) {
			append = true;
		} else {
			encodeData += "&";
		}
		encodeData += encodeURIComponent(key).replace(/%20/g, "+") + "=" +
			encodeURIComponent(data[key]).replace(/%20/g, "+");
	});
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (typeof callback == "function") callback(xmlhttp.responseText);
		}
	}
	xmlhttp.send(encodeData);
}

function getUrlVars(url) {
	var vars = [],
		hash;
	var hashes = url.slice(url.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function searchBilibili(info) {
	chrome.tabs.create({
		url: "http://www.bilibili.com/search?keyword=" + info.selectionText
	});
}

function notifyAllTabs(message) {
	chrome.windows.getAll({
		populate: true
	}, function(wins) {
		wins.forEach(function(win) {
			win.tabs.forEach(function(tab) {
				chrome.tabs.sendMessage(tab.id, message);
			});
		});
	});
}

function updateAll() {
	notifyAllTabs({
		command: "update"
	});
}

function enableAll() {
	setOption("enabled", true);
	updateAll();
}

function disableAll() {
	setOption("enabled", false);
	updateAll();
}

function checkDynamic() {
	if (getOption("dynamic") == "on") {
		getFileData("http://interface.bilibili.com/widget/getDynamic?pagesize=1", function(data) {
			var dynamic = JSON.parse(data);
			if (typeof dynamic === "object" && typeof dynamic.num === "number") {
				var content = dynamic.list[0];
				if (content.dyn_id != parseInt(getOption("lastDyn"))) {
					if (notification) chrome.notifications.clear("bh-" + notification, function() {});
					notification = (new Date()).getTime();
					var message = chrome.i18n.getMessage('followingUpdateMessage')
						.replace('%n', dynamic.num)
						.replace('%uploader', content.dyn_type == 'SPECIAL_ADDBANGUMI' ? '搬' : content.uname)
						.replace('%title', content.dyn_type == 'SPECIAL_ADDBANGUMI' ? content.stitle : content.title),
						icon = content.cover ? content.cover : "imgs/icon-256.png";
					notificationAvid["bh-" + notification] = content.aid;
					chrome.notifications.create("bh-" + notification, {
						type: "basic",
						iconUrl: icon,
						title: chrome.i18n.getMessage('noticeficationTitle'),
						message: message,
						isClickable: false,
						buttons: [{
							title: chrome.i18n.getMessage('notificationWatch')
						}, {
							title: chrome.i18n.getMessage('notificationShowAll')
						}]
					}, function() {});
					setOption("lastDyn", content.dyn_id);
				}
				if (content.dyn_id == parseInt(getOption("lastDyn"))) {
					setOption("updates", 0);
					chrome.browserAction.setBadgeText({
						text: ""
					});
				} else {
					setOption("updates", dynamic.num);
					chrome.browserAction.setBadgeText({
						text: getOption("updates")
					});
				}
			}
		});
	}
}

function resolvePlaybackLink(avPlaybackLink, callback) {
	if (!avPlaybackLink || !avPlaybackLink.durl || !avPlaybackLink.durl[0] || !avPlaybackLink.durl[0].url) return avPlaybackLink;
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open("HEAD", avPlaybackLink.durl[0].url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var url = xmlhttp.responseURL || url;
			avPlaybackLink.durl[0].url = url;
			if (typeof callback == "function") callback(avPlaybackLink);
		}
	}
	xmlhttp.send();
}

function getVideoInfo(avid, page, callback) {
	page = parseInt(page);
	var currTime = parseInt(new Date().getTime() / 1000);
	if (isNaN(page) || page < 1) page = 1;
	if (typeof viCache[avid + '-' + page] != "undefined" && currTime - viCache[avid + '-' + page]['ts'] <= 3600) {
		callback(viCache[avid + '-' + page], true);
		return true;
	}
	getFileData("http://api.bilibili.com/view?type=json&appkey=95acd7f6cc3392f3&id=" + avid + "&page=" + page, function(avInfo) {
		avInfo = JSON.parse(avInfo);
		if (typeof avInfo.code != "undefined" && avInfo.code == -503) {
			setTimeout(function() {
				getVideoInfo(avid, page, callback);
			}, 1000);<commit_msg>limit xmlhttp to local function scope<commit_after>var notification = false,
	notificationAvid = {},
	playerTabs = {},
	cidHackType = {},
	viCache = {},
	locale = 0,
	localeAcquired = false,
	localeTimeout = null;

URL.prototype.__defineGetter__('query', function() {
	var parsed = this.search.substr(1).split('&');
	var parsedObj = {};
	parsed.forEach(function(elem, iter, arr) {
		var vals = arr[iter].split('=');
		parsedObj[vals[0]] = vals[1];
	});
	return parsedObj;
});

var randomIP = function(fakeip) {
	var ip_addr = '220.181.111.';
	if (fakeip == 2) ip_addr = '59.152.193.';
	ip_addr += Math.floor(Math.random() * 254 + 1);
	return ip_addr;
}

function getFileData(url, callback, fakeip) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url, true);
	if (fakeip && locale != fakeip) {
		var ip = randomIP(fakeip);
		xmlhttp.setRequestHeader('Client-IP', ip);
		xmlhttp.setRequestHeader('X-Forwarded-For', ip);
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (typeof callback == "function") callback(xmlhttp.responseText);
		} else if (xmlhttp.status > 400) {
			if (typeof callback == "function") callback("{}");
		}
	}
	xmlhttp.send();
}

function postFileData(url, data, callback) {
	var encodeData = "",
		append = false;
	Object.keys(data).forEach(function(key) {
		if (!append) {
			append = true;
		} else {
			encodeData += "&";
		}
		encodeData += encodeURIComponent(key).replace(/%20/g, "+") + "=" +
			encodeURIComponent(data[key]).replace(/%20/g, "+");
	});
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (typeof callback == "function") callback(xmlhttp.responseText);
		}
	}
	xmlhttp.send(encodeData);
}

function getUrlVars(url) {
	var vars = [],
		hash;
	var hashes = url.slice(url.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function searchBilibili(info) {
	chrome.tabs.create({
		url: "http://www.bilibili.com/search?keyword=" + info.selectionText
	});
}

function notifyAllTabs(message) {
	chrome.windows.getAll({
		populate: true
	}, function(wins) {
		wins.forEach(function(win) {
			win.tabs.forEach(function(tab) {
				chrome.tabs.sendMessage(tab.id, message);
			});
		});
	});
}

function updateAll() {
	notifyAllTabs({
		command: "update"
	});
}

function enableAll() {
	setOption("enabled", true);
	updateAll();
}

function disableAll() {
	setOption("enabled", false);
	updateAll();
}

function checkDynamic() {
	if (getOption("dynamic") == "on") {
		getFileData("http://interface.bilibili.com/widget/getDynamic?pagesize=1", function(data) {
			var dynamic = JSON.parse(data);
			if (typeof dynamic === "object" && typeof dynamic.num === "number") {
				var content = dynamic.list[0];
				if (content.dyn_id != parseInt(getOption("lastDyn"))) {
					if (notification) chrome.notifications.clear("bh-" + notification, function() {});
					notification = (new Date()).getTime();
					var message = chrome.i18n.getMessage('followingUpdateMessage')
						.replace('%n', dynamic.num)
						.replace('%uploader', content.dyn_type == 'SPECIAL_ADDBANGUMI' ? '搬' : content.uname)
						.replace('%title', content.dyn_type == 'SPECIAL_ADDBANGUMI' ? content.stitle : content.title),
						icon = content.cover ? content.cover : "imgs/icon-256.png";
					notificationAvid["bh-" + notification] = content.aid;
					chrome.notifications.create("bh-" + notification, {
						type: "basic",
						iconUrl: icon,
						title: chrome.i18n.getMessage('noticeficationTitle'),
						message: message,
						isClickable: false,
						buttons: [{
							title: chrome.i18n.getMessage('notificationWatch')
						}, {
							title: chrome.i18n.getMessage('notificationShowAll')
						}]
					}, function() {});
					setOption("lastDyn", content.dyn_id);
				}
				if (content.dyn_id == parseInt(getOption("lastDyn"))) {
					setOption("updates", 0);
					chrome.browserAction.setBadgeText({
						text: ""
					});
				} else {
					setOption("updates", dynamic.num);
					chrome.browserAction.setBadgeText({
						text: getOption("updates")
					});
				}
			}
		});
	}
}

function resolvePlaybackLink(avPlaybackLink, callback) {
	if (!avPlaybackLink || !avPlaybackLink.durl || !avPlaybackLink.durl[0] || !avPlaybackLink.durl[0].url) return avPlaybackLink;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("HEAD", avPlaybackLink.durl[0].url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var url = xmlhttp.responseURL || url;
			avPlaybackLink.durl[0].url = url;
			if (typeof callback == "function") callback(avPlaybackLink);
		}
	}
	xmlhttp.send();
}

function getVideoInfo(avid, page, callback) {
	page = parseInt(page);
	var currTime = parseInt(new Date().getTime() / 1000);
	if (isNaN(page) || page < 1) page = 1;
	if (typeof viCache[avid + '-' + page] != "undefined" && currTime - viCache[avid + '-' + page]['ts'] <= 3600) {
		callback(viCache[avid + '-' + page], true);
		return true;
	}
	getFileData("http://api.bilibili.com/view?type=json&appkey=95acd7f6cc3392f3&id=" + avid + "&page=" + page, function(avInfo) {
		avInfo = JSON.parse(avInfo);
		if (typeof avInfo.code != "undefined" && avInfo.code == -503) {
			setTimeout(function() {
				getVideoInfo(avid, page, callback);
			}, 1000);
{'fraction_non_alphanumeric': 0.18106402164111812, 'fraction_numerical': 0.016411181244364293, 'mean_word_length': 4.080622995877233, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 49}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2073166', 'n_tokens_mistral': 4426, 'n_tokens_neox': 3885, 'n_words': 709}