getVaptchaCode

vaptcha-iframe 依赖库

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/418420/878837/getVaptchaCode.js

  1. /**
  2. * getVaptchaCode v1.0
  3. * @author Jixun<https://jixun.moe/>
  4. * @license BSD-3-Clause
  5. */
  6.  
  7. function getVaptchaCode(frameURL, container) {
  8. return new Promise((resolve) => {
  9. const {origin} = new URL(frameURL);
  10. const id = 'id' + Date.now() + Math.random();
  11. const frame = document.createElement('iframe');
  12. frame.style.transition = 'opacity 1s 1s';
  13. frame.style.opacity = 1;
  14.  
  15. const eventHandler = (event) => {
  16. if (event.data.vaptcha === id) {
  17. resolve(event.data.token);
  18. window.removeEventListener('message', eventHandler);
  19. frame.style.opacity = 0;
  20.  
  21. // Remove iframe after fadeout effects.
  22. setTimeout(() => {
  23. if (frame.parentNode) {
  24. frame.parentNode.removeChild(frame);
  25. }
  26. }, 1000);
  27. }
  28. };
  29.  
  30. window.addEventListener('message', eventHandler);
  31.  
  32. frame.src = frameURL;
  33. frame.onload = () => {
  34. frame.contentWindow.postMessage({ vaptcha: 'init', id }, origin);
  35. };
  36. container.appendChild(frame);
  37. });
  38. }