imes token refresh

从json数据中取值单独放一份到localStorage中

当前为 2021-06-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         imes token refresh
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  从json数据中取值单独放一份到localStorage中
// @author       niushuai233
// @run-at       document-start
// @match        *://*.nti56.com/*
// @require      https://unpkg.com/[email protected]/dist/ajaxhook.min.js
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var localStorage = window.localStorage;
    var token = localStorage.getItem('pro__Access-Token');
    var imestoken = '根据key[pro__Access-Token]未找到[token]'
    if (token && token.length > 0) {
         imestoken = JSON.parse(token).value;
    }
    localStorage.setItem('imes_token', imestoken);
    console.log('imes token refresh success', imestoken);

    var code_url = '/ils/sys/getCheckCode';
    var isCodeUrl = false;

    ah.proxy({
        //请求发起前进入
        onRequest: (config, handler) => {
            var url_arr = config.url.split("?")
            console.log(url_arr, new Date().toLocaleString())
            if (code_url == url_arr[0]) {
                isCodeUrl = true
            }
            handler.next(config);
        },
        //请求发生错误时进入,比如超时;注意,不包括http状态码错误,如404仍然会认为请求成功
        onError: (err, handler) => {
            console.log(err.type)
            handler.next(err)
        },
        //请求成功后进入
        onResponse: (response, handler) => {
            var res = response.response;
            if (isCodeUrl) {
                res = JSON.parse(res);
                autoFillCode(res.result.code);
                isCodeUrl = false
            }
            handler.next(response)
        }
    })

    function autoFillCode(code) {
        const usernameInput = document.querySelector('input[id=inputCode]')
        const button = document.querySelector('.login-button')

        const event = document.createEvent('HTMLEvents')
        event.initEvent('input', false, true)

        usernameInput.value = code // 修改用户名输入框的值
        usernameInput.dispatchEvent(event) // 手动触发输入框的input事件

        button.click() // 触发按钮点击事件
    }



})();