SCNU SSO auto captcha filler

scnu sso captcha auto filler using tensorflow.js 0.8~0.91 accuracy

目前為 2021-05-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        SCNU SSO auto captcha filler
// @name:zh-CN  SCNU SSO 验证码自动填充
// @namespace   https://github.com/fengkx/
// @match       https://sso.scnu.edu.cn/AccountService/openapi/login.html*
// @match       https://sso.scnu.edu.cn/AccountService/user/login.html*
// @grant       none
// @version     1.2
// @author      fengkx
// @description scnu sso captcha auto filler using tensorflow.js 0.8~0.91 accuracy
// @description:zh-CN 基于 tensorflow.js SCNU SSO 验证码自动填充, 0.8~0.91 准确率
// @supportURL https://github.com/fengkx/scnu-sso-captcha
// @run-at document-end
// @require https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js
// @require https://cdn.jsdelivr.net/npm/[email protected]/opencv.min.js
// ==/UserScript==

const WIDTH = 100
const IDICT = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const HEIGHT = 50

const $input = document.getElementById('rancode')
const img =
  document.getElementById('codeimg') || document.getElementById('code')

const $canvas = document.createElement('canvas')
$canvas.addEventListener('click', window.reloadcode)
$canvas.width = WIDTH
$canvas.height = HEIGHT
const isUseLite = navigator.userAgent.indexOf('Firefox') >= 0
const modelDir = isUseLite ? 'js-model-lite-1' : 'js-model-3'
const MODEL_URL =
  'https://cdn.jsdelivr.net/gh/fengkx/[email protected]/web-model/' +
  modelDir +
  '/model.json'
console.debug(MODEL_URL)

async function main() {
  console.log(
    `${'\n'} %c SCNU SSO auto captcha filler v1.2 %c https://github.com/fengkx/scnu-sso-captcha ${'\n'}${'\n'}`,
    'color: #000; background: #fffcc8; padding:5px 0;',
    'background: #fadfa3; padding:5px 0;'
  )
  const $img = img.cloneNode()
  $img.width = WIDTH
  $img.height = HEIGHT

  const ctx = $canvas.getContext('2d')
  $canvas.classList.add('input-addon')
  $canvas.style.right = '100px'
  $input.parentElement.appendChild($canvas)
  ctx.drawImage($img, 0, 0)
  const imageData = ctx.getImageData(0, 0, WIDTH, HEIGHT)
  const mat = cv.matFromImageData(imageData)
  let dst = new cv.Mat()
  let src = mat
  cv.cvtColor(src, src, cv.COLOR_BGR2GRAY, 0)
  cv.adaptiveThreshold(
    src,
    src,
    255,
    cv.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv.THRESH_BINARY_INV,
    15,
    15
  )
  cv.medianBlur(src, dst, 3)
  cv.imshow($canvas, dst)
  x = tf.browser.fromPixels($canvas)
  x = x.div(255)
  x = x.max(2).expandDims(2)
  console.debug(x.shape)
  x = await x.array()
  let model
  try {
    model = await tf.loadLayersModel('indexeddb://' + modelDir)
  } catch (e) {
    model = await tf.loadLayersModel(MODEL_URL)
    const saveResults = await model.save('indexeddb://' + modelDir)
  }
  const old = Date.now()
  let p = model.predict(tf.tensor([x]))
  const used = (Date.now() - old) / 1000
  console.debug(used)
  p = await p.array()
  p = tf.tensor(p[0])
  p = p.argMax(1)
  p = await p.array()
  p = p.map((idx) => IDICT[idx]).join('')
  console.log(p)
  $input.value = p
}
setTimeout(() => {
  const docState = document.readyState
  console.log(docState)
  if (docState === 'complete') {
    main()
  }
  img.addEventListener('load', main)
}, 0)