mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
(()=>{return(e,t)=>{const{NavbarComponent:i}=t.import("custom-navbar-component");const s=new class{getAll(){const e=localStorage.getItem("search_history");if(!e){return[]}return JSON.parse(e).map(e=>{return{keyword:e.value,date:new Date(e.timestamp).toJSON(),count:1}})}saveAll(e){localStorage.setItem("search_history",JSON.stringify(e))}clear(){localStorage.setItem("search_history","[]")}merge(e){const t=this.getAll();return _.uniqBy(e.concat(t),e=>e.keyword).slice(0,10)}add(e){const t=this.getAll();const i=t.find(t=>t.keyword===e.keyword);if(i){Object.assign(i,e)}else{t.push(e)}this.saveAll(t)}remove(e){const t=this.getAll();const i=t.findIndex(t=>t.keyword===e.keyword);if(i>-1){t.splice(i,1);this.saveAll(t)}}};const n=e=>{const t=e.match(/^av([\d]+)$/i);if(t){return{success:true,text:e,link:`https://www.bilibili.com/av${t[1]}`,aid:t[1],bvid:""}}const i=e.match(/^bv[\da-zA-Z]+$/i);if(i){return{success:true,text:e,link:`https://www.bilibili.com/${e.replace(/^bv/i,"BV")}`,aid:"",bvid:e.replace(/^bv/i,"BV")}}return{success:false,text:e,link:"",aid:"",bvid:""}};class a extends i{constructor(){super();this.disabled=true;this.html=`\n<form id="custom-navbar-search" autocomplete="off" target="_blank" method="get" action="https://search.bilibili.com/all">\n<input type="text" placeholder="搜索" name="keyword">\n<input type="hidden" name="from_source" value="banner_search">\n<a style="display: none" target="_blank" class="recommended-target"></a>\n<button type="submit" title="搜索" tabindex="-1">\n<svg style="width:22px;height:22px" viewBox="0 0 24 24">\n<path d="M9.5,3A6.5,6.5 0 0,1 16,9.5C16,11.11 15.41,12.59 14.44,13.73L14.71,14H15.5L20.5,19L19,20.5L14,15.5V14.71L13.73,14.44C12.59,15.41 11.11,16 9.5,16A6.5,6.5 0 0,1 3,9.5A6.5,6.5 0 0,1 9.5,3M9.5,5C7,5 5,7 5,9.5C5,12 7,14 9.5,14C12,14 14,12 14,9.5C14,7 12,5 9.5,5Z" />\n</svg>\n</button>\n</form>\n<div class="popup search-list" :class="{empty: items.length === 0}">\n<div\n class="search-list-item"\n tabindex="0"\n v-for="(item, index) of items"\n :title="isHistory ? item.html : ''"\n @keydown.enter="submit(item.value)"\n @keydown.shift.delete="deleteItem(item, index)"\n @keydown.down.prevent="nextItem(index)"\n @keydown.up.prevent="previousItem(index)">\n<div\n @click.self="submit(item.value)"\n class="search-list-item-text"\n :title="item.value"\n v-html="item.html"></div>\n<div\n class="delete-history"\n v-if="isHistory"\n title="删除此项"\n @click="deleteItem(item, index)">\n<i class="mdi mdi-18px mdi-close"></i>\n</div>\n</div>\n<div\n class="search-list-item clear-history"\n tabindex="0"\n v-if="items.length > 0 && isHistory"\n @click="clearSearchHistory()"\n @keydown.enter="clearSearchHistory()"\n @keydown.down.prevent="nextItem(items.length)"\n @keydown.up.prevent="previousItem(items.length)">\n<i class="mdi mdi-18px mdi-delete-sweep"></i>\n 清除搜索历史\n</div>\n<div class="copy-tip" :class="{show: showCopyTip}">\n 已复制\n</div>\n</div>\n`;this.init()}async init(){const t=await SpinQuery.select("#custom-navbar-search");const i=t.querySelector("input[name='keyword']");t.addEventListener("submit",s=>{if(i.value===""){if(!e.hideTopSearch){t.querySelector(".recommended-target").click()}s.preventDefault();return false}const a=n(i.value);if(a.success){window.open(a.link,"_blank");s.preventDefault();return false}const r=e.searchHistory.find(e=>e.keyword===i.value);if(r){r.count++;r.date=(new Date).toJSON()}else{const t={count:1,keyword:i.value,date:(new Date).toJSON()};e.searchHistory.unshift(t)}e.searchHistory=e.searchHistory.slice(0,10);return true});if(!e.hideTopSearch){const e=await Ajax.getJson("https://api.bilibili.com/x/web-interface/search/default");if(e.code===0){i.setAttribute("placeholder",e.data.show_name);let s;if(e.data.url!==""){s=e.data.url}else if(e.data.name.startsWith("av")){s=`https://www.bilibili.com/${e.data.name}`}else{s=`https://search.bilibili.com/all?keyword=${e.data.name}`}t.querySelector(".recommended-target").setAttribute("href",s)}else{console.error("[自定义顶栏] 获取搜索推荐词失败")}}const s=new Vue({el:dq(".popup.search-list"),data:{items:[],isHistory:true,showCopyTip:false},methods:{closeCopyTip:_.debounce(function(){this.showCopyTip=false},2e3),copy(e){GM.setClipboard(e,"text");this.showCopyTip=true;this.closeCopyTip()},submit(e){if(n(e).success){this.copy(e)}else{i.value=e;t.submit();raiseEvent(t,"submit")}},nextItem(e){const t=dq(`.custom-navbar .search-list-item:nth-child(${e+2})`);if(t){t.focus()}},previousItem(e){const t=dq(`.custom-navbar .search-list-item:nth-child(${e})`);if(t){t.focus()}else{i.focus();return}},deleteItem(t,s){if(i.value!==""){return}const n=e.searchHistory.findIndex(e=>e.keyword===t.value);const[a]=e.searchHistory.splice(n,1);e.searchHistory=e.searchHistory;this.items.splice(s,1)},clearSearchHistory(){e.searchHistory=[];this.items=[]}}});let a="";const r=async()=>{const t=i.value;s.isHistory=t==="";const r=n(t);if(s.isHistory){s.items=e.searchHistory.sort((e,t)=>{const i=e.date?new Date(e.date):new Date(0);const s=t.date?new Date(t.date):new Date(0);return Number(s)-Number(i)}).map(e=>{return{value:e.keyword,html:e.keyword}}).slice(0,10)}else if(r.success){s.items=[];const e=r.aid?`https://api.bilibili.com/x/web-interface/view?aid=${r.aid}`:`https://api.bilibili.com/x/web-interface/view?bvid=${r.bvid}`;const t=await Ajax.getJson(e);if(r.aid){const e=_.get(t,"data.bvid",null);if(e!==null){s.items=[{value:e,html:`复制BV号: ${e}`}]}}else if(r.bvid){const e=_.get(t,"data.aid",null);if(e!==null){s.items=[{value:"av"+e,html:`复制AV号: av${e}`}]}}}else{const e=`https://s.search.bilibili.com/main/suggest?func=suggest&suggest_type=accurate&sub_type=tag&main_ver=v1&highlight=&userid=${getUID()}&bangumi_acc_num=1&special_acc_num=1&topic_acc_num=1&upuser_acc_num=3&tag_num=10&special_num=10&bangumi_num=10&upuser_num=3&term=${t}`;a=e;const i=await Ajax.getJson(e);if(i.code!==0||a!==e){return}const n=i.result.tag;if(n===undefined){s.items=[];return}s.items=n.map(e=>{return{value:e.value,html:e.name.replace(/suggest_high_light/g,"suggest-highlight")}})}};r();const c=_.debounce(r,200);let o=false;i.addEventListener("compositionstart",()=>o=true);i.addEventListener("compositionend",()=>{o=false;raiseEvent(i,"input")});i.addEventListener("input",()=>{if(!o){c()}});i.addEventListener("keydown",e=>{if(e.key==="ArrowDown"&&s.items.length>0){e.preventDefault();dq(".custom-navbar .search-list-item:first-child").focus()}})}get name(){return"search"}}return{export:{SearchBox:a}}}})(); |