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