Skip to content

Instantly share code, notes, and snippets.

@glukki
Last active August 29, 2015 14:04
Show Gist options
  • Save glukki/d9d61709dc9c1f4354d8 to your computer and use it in GitHub Desktop.
Save glukki/d9d61709dc9c1f4354d8 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Free WiFi Reconnect
// @namespace http://glukki.ru/
// @description Detects free Wi-Fi timelimit expiration and logs you back in
// @version 0.0.5
// @updateURL https://gist.githubusercontent.com/glukki/d9d61709dc9c1f4354d8/raw/FreeWifiReconnect.js
// @match http://www.apple.com/library/test/success.html
// @match *://login.connectum.ru/*
// @grant unsafeWindow
// @copyright 2014+, Vitaliy Meschaninov
// ==/UserScript==
var Logic = function(options){
this._window = options.window;
};
Logic.prototype.CHECK_URL = "http://www.apple.com/library/test/success.html";
Logic.prototype.SUCCESS = "Success";
Logic.prototype.INTERVAL = 30 * 1000; // 30 seconds
Logic.prototype.reload = function() {
this._window.location = this.CHECK_URL;
};
Logic.prototype.checkSuccess = function() {
if (this._window && this._window.document && this._window.document.title === this.SUCCESS) {
return true;
}
return false;
};
Logic.prototype.connect = function() {
var strategy = this.strategy[this._window.location.hostname];
if (!strategy) {
return alert("We have no strategy for this network");
}
if (typeof strategy === "object") {
var keys = Object.keys(strategy);
for (var i = 0; i < keys.length; i += 1) {
if (this._window.location.pathname.indexOf(strategy[keys[i]]) !== -1){
strategy = strategy[keys[i]];
break;
}
}
if(typeof strategy !== "string") {
return alert("We have no matching strategy for this network");
}
}
var element;
var type = strategy[0];
strategy = strategy.substr(1);
if (type === "#") {
element = this._window.document.getElementById(strategy);
} else if (type === ".") {
element = this._window.document.getElementsByClassName(strategy)[0]
} else {
return alert("Selector not supported");
}
if (!element) {
return alert("Cant get required element");
}
element.click();
};
Logic.prototype.run = function() {
if (!this.checkSuccess()) {
return this.connect();
}
setTimeout(this.reload.bind(this), this.INTERVAL);
};
/**
* Click rules
* hostname : selector
* OR
* hostname : pathname (substring) : selector
*/
Logic.prototype.strategy = {
"login.connectum.ru": ".connectbutton"
};
(function() {
var instance = new Logic({
window: unsafeWindow
});
instance.run();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment