Skip to content

Instantly share code, notes, and snippets.

@JasonHK
Created February 10, 2019 11:04
Show Gist options
  • Save JasonHK/7766737fb4ad621342e16d0aa6cdced4 to your computer and use it in GitHub Desktop.
Save JasonHK/7766737fb4ad621342e16d0aa6cdced4 to your computer and use it in GitHub Desktop.
MCBBS Login Fix
// ==UserScript==
// @name MCBBS Login Fix
// @description Fixed the annoying CAPTCHA bug when logging in to MCBBS.
// @author JasonHK
// @namespace http://jasonhk.net/
// @version 0.2-beta
// @match http*://www.mcbbs.net/*
// @run-at document-start
// @grant none
// @supportURL https://greasyfork.org/zh-TW/scripts/377605-mcbbs-login-fix
// @updateURL https://greasyfork.org/scripts/377605-mcbbs-login-fix/code/MCBBS%20Login%20Fix.meta.js
// @downloadURL https://greasyfork.org/scripts/377605-mcbbs-login-fix/code/MCBBS%20Login%20Fix.user.js
// ==/UserScript==
"use strict";
Object.defineProperty(window, "InitCaptcha", {
writeable: false,
value: ((element, callback) => {
let handlerEmbed = ((captchaObj) => {
let a = window.$(element);
captchaObj.appendTo(a);
if (element == "gt_forumdisplay_postbutton_top" || element == "gt_viewthread_fastpost_content" || element=="gt_viewthread_modaction") {
captchaObj.onSuccess(() => {
setTimeout(function(){captchaObj.reset()},5000);
});
}
});
let loadGeetest = ((config) => {
window.initGeetest({
gt: config.gt,
challenge: config.challenge,
new_captcha: config.new_captcha,
product: "embed",
offline: !config.success
}, handlerEmbed);
});
fetch(`/plugin.php?id=geetest3&model=start&t=${ new Date().getTime() }`, {
method: "GET"
}).then((response) => {
if (! response.ok) throw response.error();
return response.json();
}).then((data) => {
console.log(data);
loadGeetest(data);
}).catch((error) => {
if (error instanceof Error) {
switch (error.name) {
case "SyntaxError":
const regexp = /^Unexpected token (\S+?) in (JSON) at position (\d+?)$/g;
if (regexp.test(error.message)) {
console.error("Unable to fetch the CAPTCHA API, refreshing and try again...");
location.reload();
}
break;
default:
throw error;
}
} else if (error instanceof Response) {
throw new Error(`HTTP ${ error.status }: ${ error.statusText }`);
} else {
throw new Error(error);
}
}).catch((error) => {
console.error(`Sorry, an unexpected error has occurred!\n ${ error }`);
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment