automatic_insert_totp_code

10/19/2021, 3:22:51 PM

  1. // ==UserScript==
  2. // @name automatic_insert_totp_code
  3. // @namespace Violentmonkey Scripts
  4. // @match https://access.ynu.edu.cn/shterm/login?tokenRequest
  5. // @grant none
  6. // @version 0.2.2
  7. // @author liudonghua123
  8. // @license MIT
  9. // @description 10/19/2021, 3:22:51 PM
  10. // ==/UserScript==
  11.  
  12. // https://stackoverflow.com/questions/5132488/how-can-i-insert-a-script-into-html-head-dynamically-using-javascript
  13.  
  14. const dynamicAddScript = (url) => {
  15. return new Promise(function(resolve, reject){
  16. const script = document.createElement('script');
  17. script.onload = resolve;
  18. script.onerror = reject;
  19. script.src = url;
  20. document.head.appendChild(script);
  21. });
  22. }
  23.  
  24. function sleep(time){
  25. return new Promise(function(resolve){
  26. setTimeout(resolve, time);
  27. });
  28. }
  29.  
  30. // https://github.com/yeojz/otplib
  31. //dynamicAddScript('https://unpkg.com/@otplib/preset-browser@^12.0.0/buffer.js');
  32. //dynamicAddScript('https://unpkg.com/@otplib/preset-browser@^12.0.0/index.js');
  33. //window.otplib.totp.generate('EXGVVAJJ4SI2AEEX')
  34. //
  35. // https://github.com/LanceGin/jsotp
  36. // no browser support ?
  37.  
  38. // const totp_secret = 'REPLACE_YOUR_TOTP_SECRET_HERE'
  39. // Execute the following js code to config the totp secret
  40. // localStorage.secret = 'REPLACE_YOUR_TOTP_SECRET_HERE'
  41.  
  42. (async function process(secret) {
  43. // https://github.com/hectorm/otpauth
  44. //
  45. // ERR_CONNECTION_RESET
  46. // await dynamicAddScript('https://cdn.jsdelivr.net/npm/otpauth/dist/otpauth.umd.min.js');
  47. //
  48. // https://unpkg.com/
  49. // You may also use a semver range or a tag instead of a fixed version number, or omit the version/tag entirely to use the latest tag.
  50. // await dynamicAddScript('https://unpkg.com/otpauth/dist/otpauth.umd.min.js');
  51. await dynamicAddScript('https://cdnjs.cloudflare.com/ajax/libs/otpauth/9.2.1/otpauth.umd.min.js');
  52. // await sleep(1000);
  53. const totp = new OTPAuth.TOTP({
  54. // issuer: 'access.ynu.edu.cn',
  55. // label: 'access.ynu.edu.cn',
  56. // algorithm: 'SHA1',
  57. // digits: 6,
  58. // period: 30,
  59. secret, // or "OTPAuth.Secret.fromBase32('NB2W45DFOIZA')"
  60. });
  61. // Generate a token.
  62. let token = totp.generate();
  63. console.info(`totp.generate code ${token}`);
  64. document.querySelector('#loginDiv > div > section.login_dynamic_code_wrap > form > div.form-group.form-control.dynamic_code > input[type=password]').value = token;
  65. })(localStorage.secret);