<commit_before>function checkForInternet() {
	console.log("checking for internet connection...");
	
	pingGoogle(function() {
		console.log("we are online");
		chrome.browserAction.setBadgeText({text: "OK"});
		chrome.browserAction.setIcon({path: "icon.png"});
		
	}, function(respText) {
		console.log("no internet!")
		chrome.browserAction.setBadgeText({text: ""});
		chrome.browserAction.setIcon({path: "icon-offline.png"});
							
		// Try find a token in the NU-NET auth page
		var token = grabTokenNuNet(respText);
				
		// If found then lets attempt an auth
		if (token) {
			console.log("NU-NET detected: " + token);
			connectNuNet(token);
		}
	});
}

function pingGoogle(success, fail) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", "http://clients3.google.com/generate_204", true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status === 204) {
				success();
			}
			else {
				fail(xhr.responseText);
			}
		}
	};
	xhr.send();
}

function grabTokenNuNet(respText) {
	var pattern = new RegExp('magic" value="[a-z0-9]+');
	var token = pattern.exec(respText);
	if (token === null)
		return null;
	
	token = token[0].substr(14);
	return token;
}

function connectNuNet(token) {
	console.log("connecting to NU-NET");

    var username = localStorage["username"];
    var password = localStorage["password"];
	
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "https://login.nu.ac.th:1000/", true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			pingGoogle(function() {
				console.log("connect success");
				chrome.browserAction.setBadgeText({text: "OK"});
				chrome.browserAction.setIcon({path: "icon.png"});
			}, function(respText) {
				console.log("connect unsuccessful, maybe not NU");
			});
		}
	};
	xhr.send("magic="+token+"&username="+username+"&password="+password);
}
	
function connectNuWireless() {
	
}

	
chrome.browserAction.setBadgeText({text: ""});
chrome.browserAction.setIcon({path: "icon-offline.png"});

chrome.browserAction.onClicked.addListener(function() {
	chrome.tabs.create({url: "options.html"});
});


// Set up alarm to check internet every minute
chrome.alarms.onAlarm.addListener(function() {
  checkForInternet();
});
chrome.alarms.create({when: 0, periodInMinutes: 1});
<commit_msg>Removed badges. Added check for internet on tab open.<commit_after>function checkForInternet() {
	console.log("checking for internet connection...");
	
	pingGoogle(function() {
		console.log("we are online");
		chrome.browserAction.setIcon({path: "icon.png"});
		
	}, function(respText) {
		console.log("no internet!")
		chrome.browserAction.setIcon({path: "icon-offline.png"});
							
		// Try find a token in the NU-NET auth page
		var token = grabTokenNuNet(respText);
				
		// If found then lets attempt an auth
		if (token) {
			console.log("NU-NET detected: " + token);
			connectNuNet(token);
		}
	});
}

function pingGoogle(success, fail) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", "http://clients3.google.com/generate_204", true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status === 204) {
				success();
			}
			else {
				fail(xhr.responseText);
			}
		}
	};
	xhr.send();
}

function grabTokenNuNet(respText) {
	var pattern = new RegExp('magic" value="[a-z0-9]+');
	var token = pattern.exec(respText);
	if (token === null)
		return null;
	
	token = token[0].substr(14);
	return token;
}

function connectNuNet(token) {
	console.log("connecting to NU-NET");

    var username = localStorage["username"];
    var password = localStorage["password"];
	
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "https://login.nu.ac.th:1000/", true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			pingGoogle(function() {
				console.log("connect success");
				chrome.browserAction.setIcon({path: "icon.png"});
			}, function(respText) {
				console.log("connect unsuccessful, maybe not NU");
			});
		}
	};
	xhr.send("magic="+token+"&username="+username+"&password="+password);
}
	
function connectNuWireless() {
	
}

// Default icon is offline
chrome.browserAction.setIcon({path: "icon-offline.png"});

// Open options on button click
chrome.browserAction.onClicked.addListener(function() {
	chrome.tabs.create({url: "options.html"});
});

// Check for internet when the first tab is loaded
chrome.tabs.onCreated.addListener(function() {
  checkForInternet();
});

// Set up alarm to check internet every 3 minutes
chrome.alarms.onAlarm.addListener(function() {
  checkForInternet();
});
chrome.alarms.create({when: 0, periodInMinutes: 3});

{'fraction_non_alphanumeric': 0.1707983193277311, 'fraction_numerical': 0.008613445378151261, 'mean_word_length': 4.313616071428571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4281301', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1574, 'n_words': 315}