mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
6.9 KiB
JavaScript
1 line
6.9 KiB
JavaScript
(()=>(e,t)=>{const{NavbarComponent:i}=t.import("custom-navbar-component");const s="be_search_history";const n="search_history";const a=10;const o=(e=n)=>{const t=localStorage.getItem(e);const i=t?JSON.parse(t):[];return i};const c=(e=n)=>localStorage.setItem(e,"[]");const l=(e,t=n)=>{localStorage.setItem(t,JSON.stringify(_.sortBy(_.uniqBy(o().concat(e),(e=>e.value)),(e=>e.timestamp)).reverse().slice(0,a)))};const r=(e,t=n)=>{const i=o();const s=i.findIndex((t=>t.value===e));if(s!==-1){i.splice(s,1);localStorage.setItem(t,JSON.stringify(i))}};const m=()=>{if(e.searchHistory.length>0){try{const t=o();const i=e.searchHistory.map((e=>({value:e.keyword,isHistory:1,timestamp:Number(new Date(e.date))})));const s=_.sortBy(_.uniqBy(i.concat(t),(e=>e.value)),(e=>e.timestamp)).reverse().slice(0,a);localStorage.setItem(n,JSON.stringify(s));e.searchHistory=[]}catch(e){console.error(e)}}if(o(s).length>0){o(s).forEach((e=>l(e)));c(s)}};const u=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 d 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="nav_suggest_new">\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.stop.prevent="submit(item.value)"\n @keydown.shift.delete.stop.prevent="deleteItem(item, index)"\n @keydown.down.stop.prevent="nextItem(index)"\n @keydown.up.stop.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.stop.prevent="clearSearchHistory()"\n @keydown.down.stop.prevent="nextItem(items.length)"\n @keydown.up.stop.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']");m();if(document.URL.startsWith("https://search.bilibili.com")){var s;const e=((s=window.location.search.match(/keyword=([^&]+)/))===null||s===void 0?void 0:s[1])||"";i.value=decodeURIComponent(e);const t=dq("#search-keyword");if(t){t.addEventListener("change",(()=>i.value=t.value))}}t.addEventListener("submit",(s=>{if(i.value===""){if(!e.hideTopSearch){t.querySelector(".recommended-target").click()}else{window.open("https://search.bilibili.com")}s.preventDefault();return false}const n=u(i.value);if(n.success){window.open(n.link,"_blank");s.preventDefault();return false}const a=o().find((e=>e.value===i.value));if(a){a.timestamp=Number(new Date)}else{const e={value:i.value,isHistory:1,timestamp:Number(new Date)};l(e)}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 n=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(u(e).success){this.copy(e)}else{i.value=e;if(t.requestSubmit){t.requestSubmit()}else{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(e,t){if(i.value!==""){i.value=""}r(e.value);this.items.splice(t,1)},clearSearchHistory(){c();this.items=[]}}});let d="";const p=async()=>{const e=i.value;n.isHistory=e==="";const t=u(e);if(n.isHistory){n.items=o().sort(((e,t)=>t.timestamp-e.timestamp)).map((e=>({value:e.value,html:e.value}))).slice(0,a)}else if(t.success){n.items=[];const e=t.aid?`https://api.bilibili.com/x/web-interface/view?aid=${t.aid}`:`https://api.bilibili.com/x/web-interface/view?bvid=${t.bvid}`;const i=await Ajax.getJson(e);if(t.aid){const e=_.get(i,"data.bvid",null);if(e!==null){n.items=[{value:e,html:`复制BV号: ${e}`}]}}else if(t.bvid){const e=_.get(i,"data.aid",null);if(e!==null){n.items=[{value:"av"+e,html:`复制AV号: av${e}`}]}}}else{const t=`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=${e}`;d=t;const i=await Ajax.getJson(t);if(i.code!==0||d!==t){return}const s=i.result.tag;if(s===undefined){n.items=[];return}n.items=s.map((e=>({value:e.value,html:e.name.replace(/suggest_high_light/g,"suggest-highlight")})))}};p();const h=_.debounce(p,200);let v=false;i.addEventListener("compositionstart",(()=>v=true));i.addEventListener("compositionend",(()=>{v=false;raiseEvent(i,"input")}));i.addEventListener("input",(()=>{if(!v){h()}}));i.addEventListener("keydown",(e=>{if(e.key==="ArrowDown"&&n.items.length>0){e.preventDefault();e.stopImmediatePropagation();dq(".custom-navbar .search-list-item:first-child").focus()}}))}get name(){return"search"}}return{export:{SearchBox:d}}})(); |