给我聚焦

让页面的主输入框自动获取焦点

  1. // ==UserScript==
  2. // @name 给我聚焦
  3. // @namespace Focus it pls
  4. // @match https://m.weibo.cn/search
  5. // @grant none
  6. // @version 0.0.1
  7. // @author 稻米鼠
  8. // @run-at document-end
  9. // @created 2020/11/11 上午8:28:17
  10. // @update 2020/11/11 上午8:28:17
  11. // @description 让页面的主输入框自动获取焦点
  12. // ==/UserScript==
  13. const rules = [
  14. {
  15. urlReg: /^https?:\/\/m\.weibo\.cn\/search/i,
  16. elSelector: '#app input[type=search]'
  17. }
  18. ]
  19. const focusIt = (elSelector, times)=>{
  20. times = times ? times : 0
  21. const el = document.body.querySelector(elSelector)
  22. if(el){
  23. el.focus()
  24. return
  25. }
  26. if(times < 1000){
  27. times++
  28. window.setTimeout(()=>{
  29. focusIt(elSelector, times)
  30. }, 500)
  31. }
  32. }
  33. for(const rule of rules){
  34. if(rule.urlReg.test(window.location.href)){
  35. focusIt(rule.elSelector, rule.mode)
  36. break;
  37. }
  38. }