kidzgy
火狐狸
火狐狸
  • UID35190
  • 注册日期2011-02-03
  • 最后登录2024-03-28
  • 发帖数248
  • 经验196枚
  • 威望0点
  • 贡献值122点
  • 好评度17点
阅读:1635回复:2

如何用脚本对jkforum的登录页自动填单?

楼主#
更多 发布于:2023-09-15 14:23
我之前在本社区发过类似的帖子:https://www.firefox.net.cn/read.php?tid=121467
 lonely_8老哥给了一段代码:
const autoFill = (selector, type, value) => {
  const animationName = btoa(Math.random()).replace(/[^a-z]/ig, 'a');
  const style = document.createElement('style');
  style.textContent = `
      @keyframes ${animationName} {from{opacity:.9;}to{opacity:1;}}
      ${selector} {animation: ${animationName} 1ms;}
  `;
  
  const animationstart = (evt) => {
    if (evt.animationName !== animationName) return;
    const input = evt.target;
    input.focus();
    if (type === 'input') {
      Object.getOwnPropertyDescriptor(
        HTMLInputElement.prototype, 'value'
      ).set.call(input, value);
      input.dispatchEvent(new Event('input', {
        bubbles: true
      }));
    } else if (type === 'click') {
      input.click();
    }
    else if (type === 'focus') {
      setTimeout(() => {input.focus()}, 1500);
    }
  }
  addEventListener('animationstart', animationstart);
  document.head.appendChild(style);
}
 
if (location.hostname.indexOf('baidu.com') != -1){
autoFill('[name="username"]', 'input', '用户名');
autoFill('[name="password"]', 'input', '密码');
autoFill('[name="cookietime"]', 'click');
autoFill('#verifyCode',  'focus');
}

确实非常好用,可以适用99%的登录页。


除了脚本,我另外试用过lastpass以及bitwarden,发现都不好用。我的诉求是这样的:
1、打开登录页,实现自动填单,自动勾选记住密码,至于是否自动登录,可由使用者自行选择。
2、支持小弹窗,以及页面切换(比如很多外国网站是要先输入帐户,按了回车才输入密码,微软和谷歌就是这样)的自动填单
3、安全性是最不需要考虑的,因为压根没什么值钱的东西。即便有黑客黑我电脑要帐号密码,他所盗取东西的价值甚至比不过他的时间成本。


问题:
https://www.jkforum.net/member.php?mod=logging&action=login 这样的登录页,能否通过改善以上的代码,实现自动登录?
taoww
非常火狐
非常火狐
  • UID39284
  • 注册日期2013-03-18
  • 最后登录2024-04-25
  • 发帖数627
  • 经验573枚
  • 威望0点
  • 贡献值110点
  • 好评度99点
1楼#
发布于:2023-09-21 19:14
const wait = (root, selector, callback)=> {
  if (typeof root === "string") {
    callback = selector;
    selector = root;
    root = document;
  }
  if (root.querySelector(selector) !== null) {
    callback();
  } else {
    setTimeout(()=>{ wait(root, selector, callback); }, 100);
  }
}
const autoFill = (root, selector, type, value) => {
  if (typeof root === "string") {
    value = type;
    type = selector;
    selector = root;
    root = document;
  }
  const fill = (input) => {
    input.focus();
    if (type === 'input') {
      Object.getOwnPropertyDescriptor(
        HTMLInputElement.prototype, 'value'
      ).set.call(input, value);
      input.dispatchEvent(new Event('input', {
        bubbles: true
      }));
    } else if (type === 'click') {
      input.click();
    } else if (type === 'focus') {
      setTimeout(() => {input.focus()}, 1500);
    }
  }
  if (root.nodeType !== root.DOCUMENT_FRAGMENT_NODE) {
    const animationName = btoa(Math.random()).replace(/[^a-z]/ig, 'a');
    const style = document.createElement('style');
    style.textContent = `
        @keyframes ${animationName} {from{opacity:.9;}to{opacity:1;}}
        ${selector} {animation: ${animationName} 1ms;}
    `;
    const animationstart = (evt) => {
      if (evt.animationName !== animationName) return;
      fill(evt.target);
    }
    addEventListener('animationstart', animationstart);
    (root.head || root).appendChild(style);
  } else {
    fill(root.querySelector(selector));
  }
}
 
if (location.hostname.indexOf('baidu.com') != -1){
  autoFill('[name="username"]', 'input', '用户名');
  autoFill('[name="password"]', 'input', '密码');
  autoFill('[name="cookietime"]', 'click');
  autoFill('#verifyCode',  'focus');
} else if (location.hostname.indexOf('jkforum.net') != -1) {
  wait('#pan-f2e', ()=>{
    let root = document.querySelector('#pan-f2e').shadowRoot;
    autoFill(root, "#pan-body .other>button:nth-child(2)", 'click');
    wait(root, "input[type=password]", ()=>{
      autoFill(root, "input[type=text]", 'input', '用户名');
      autoFill(root, "input[type=password]", 'input', '密码');
      wait(root, "button:not([disabled])", ()=>{
        autoFill(root, "button", 'click');
      });
    });
  });
}
差不多这样吧
kidzgy
火狐狸
火狐狸
  • UID35190
  • 注册日期2011-02-03
  • 最后登录2024-03-28
  • 发帖数248
  • 经验196枚
  • 威望0点
  • 贡献值122点
  • 好评度17点
2楼#
发布于:2023-09-21 19:51
taoww:const wait = (root, selector, callback)=> {
  if (typeof root === "string") {
    callback = selector;
    selecto...
回到原帖
哇塞,管用!太帅了!!!谢谢!
游客

返回顶部