Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created November 1, 2018 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisetc/45be3783a293cb33928c8c539b4024bd to your computer and use it in GitHub Desktop.
Save wisetc/45be3783a293cb33928c8c539b4024bd to your computer and use it in GitHub Desktop.
Chrome extension site auth save.
function Site(host, loginUrl, jqEls) {
this.host = host;
this.loginUrl = loginUrl;
this.usernameEl = jqEls.usernameEl;
this.passwordEl = jqEls.passwordEl;
this.captchaEl = jqEls.captchaEl;
this.buttonEl = jqEls.buttonEl;
}
Site.prototype.saveUserAuth = function(key) {
var username = this.usernameEl.val();
var password = this.passwordEl.val();
var captcha = this.captchaEl.val();
if (username && password && captcha) {
var userAuth = {
username: username,
password: password,
};
var obj = {};
obj[key] = userAuth;
chrome.storage.sync.set(obj, function() {
console.log('%s was saved.', key);
});
}
};
Site.prototype.autoFill = function(key, result) {
var userAuth = result[key];
if (userAuth) {
var username = userAuth.username;
var password = userAuth.password;
this.usernameEl.val(username);
this.passwordEl.val(password);
}
};
Site.prototype.onInit = function() {
if (new RegExp(this.host).test(window.location.host)) {
if (window.location.href.includes(this.loginUrl)) {
var userAuthKey = this.host + '[auth]';
chrome.storage.sync.get(
[userAuthKey],
this.autoFill.bind(this, userAuthKey)
);
this.buttonEl.click(this.saveUserAuth.bind(this, userAuthKey));
}
}
};
$(function() {
// handleSiteBLogin();
var siteA = new Site('www.trgj33.com', '//www.trgj33.com/login.html', {
usernameEl: $('#j_username'),
passwordEl: $('#j_password'),
captchaEl: $('#j_captcha'),
buttonEl: $('#j_button'),
});
var siteB = new Site('qm5560.com', '//qm5560.com', {
usernameEl: $(document.getElementById('loginForm[userName]')),
passwordEl: $('#password'),
captchaEl: $(document.getElementById('loginForm[captcha]')),
buttonEl: $('#loginBtn'),
});
siteA.onInit();
siteB.onInit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment