b站|bilibili收藏列表搜索过滤

收藏视频时,添加一个输入框,允许根据关键词搜索出相关的收藏列表,过滤掉无关的

  1. // ==UserScript==
  2. // @name b站|bilibili收藏列表搜索过滤
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 收藏视频时,添加一个输入框,允许根据关键词搜索出相关的收藏列表,过滤掉无关的
  6. // @author iamqiz
  7. // @match http*://www.bilibili.com/video*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=www.bilibili.com
  9. // @run-at document-body
  10. // @license AGPLv3
  11. // @grant none
  12. // ==/UserScript==
  13. /*
  14. 给收藏弹窗添加搜索框,
  15. 原理是给document设置一个click事件,不断检测是否有收藏窗口出现, 如果出现,则插入搜索框,然后删除该click事件
  16.  
  17. */
  18. (function() {
  19. 'use strict';
  20.  
  21. // Your code here...
  22. function get_xpath_result_list(xpath){
  23. if(typeof xpath=="undefined"||xpath=="-h"){
  24. console.log(`xpath(\`//*[text()="xxx"]\``)
  25. console.log(`xpath(\`//*[contains(text(),"xxx")]\`)`)
  26. return
  27. }
  28. let xpath_results=document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null)
  29. let xpath_result_list=[]
  30. var a_result= xpath_results.iterateNext();
  31. while (a_result) {
  32. xpath_result_list.push(a_result)
  33. a_result= xpath_results.iterateNext();
  34. }
  35. return xpath_result_list
  36. }
  37. function log(msg){
  38. console.log(msg)
  39. }
  40.  
  41. // console.log(`开始油管收藏搜索V2`)
  42. let xpath=get_xpath_result_list
  43. let starttimer
  44.  
  45. let searchTimer
  46. let savelistTimer
  47. let clickFn=()=>{
  48. clearInterval(savelistTimer)
  49. if(document.querySelector(`input#qz-search-box`)){
  50. console.log(`已经存在`)
  51. return
  52. }
  53. let cnt=0
  54. savelistTimer=setInterval(()=>{
  55. cnt++
  56. let savelist= document.querySelector(`body>div#app>div.video-container-v1>.bili-dialog-m>.bili-dialog-bomb>.collection-m-exp`)
  57. if(savelist){
  58. clearInterval(savelistTimer)
  59. console.log('找到了')
  60. console.log(savelist)
  61. console.log(savelist.style)
  62. //销毁
  63. //document.removeEventListener('click',clickFn)
  64. // 加上搜索框
  65. if(1){
  66. let b=document.createElement("div")
  67. b.innerHTML=`<input type="text" id="qz-search-box" value="" placeholder="输入进行搜索" autocomplete="off" style="color: black;font-size: large; padding-left: 6px;"></input>`
  68. let c=b.firstElementChild
  69. savelist.insertAdjacentElement('afterbegin',b)
  70. let list0=b.nextElementSibling.nextElementSibling
  71. let list=list0 && list0.querySelector(`div.group-list>ul`)
  72. if(!list){
  73. console.log(`收藏列表不存在`)
  74. return
  75. }
  76. console.log(`收藏列表`)
  77. // console.log(list)
  78. c.addEventListener("input",(e)=>{
  79. console.log(`v:${e.target.value}`);
  80. if(1){
  81. //
  82. log(`清除搜索:${searchTimer}`)
  83. clearTimeout(searchTimer)
  84. searchTimer= setTimeout(()=>{
  85. log(`执行timer:${searchTimer}`)
  86. // let words=e.target.value.split(/\s+/)
  87. let words=e.target.value.trim().split(/\s+/)
  88. console.log(`搜索${words.length}|${words}|`);
  89. console.log(list);
  90.  
  91. list.childNodes.forEach((ele,index)=>{
  92. let ismatch=true
  93. if(ele.nodeType!==1){
  94. return
  95. }
  96. // let txt=ele.textContent.trim()
  97. let txt=ele.innerText.trim()
  98. // log(`${index}|txt:${txt}`)
  99. for(let word of words){
  100. if(!txt.includes(word)){
  101. ismatch=false
  102. break
  103. }
  104. }
  105. if(ismatch){
  106. ele.style.display=''
  107. }else{
  108. ele.style.display='none'
  109. }
  110. })
  111. },500)
  112. log(`创建timer:${searchTimer}`)
  113. }
  114. })
  115. let closediv=document.createElement('div')
  116. closediv.innerHTML=`<input type="button" value="重置" style="color: black;font-size: large;"/>`
  117. closediv.firstElementChild.addEventListener('click',(e)=>{
  118. console.log(`重置按钮:${Math.random()}`)
  119. console.log(e.target)
  120. // e.target.parentElement.style.display='none'
  121. console.log(`qzid:youtube-save-list 重置`);
  122. // console.log(list)
  123. c.value=""
  124. list.childNodes.forEach((ele,index)=>{
  125. // console.log(`${index}:${ele.nodeType} ${ele.innerText}`)
  126. if(ele.nodeType===1){
  127. ele.style.display=''
  128. }
  129. })
  130. })
  131. b.appendChild(closediv.firstElementChild)
  132. }
  133. }else{
  134. if(cnt>=5){
  135. console.log(`超时还未找到`)
  136. clearInterval(savelistTimer)
  137. }else{
  138. //没找到
  139. console.log(`idx:${cnt}| 没找到`)
  140. }
  141. }
  142. },200)
  143.  
  144. }
  145.  
  146. setTimeout(()=>{
  147. console.log(`开始监听 收藏搜索框`)
  148. document.addEventListener("click",clickFn)
  149. },5000)
  150.  
  151.  
  152. })();