Greasy Fork is available in English.
非百度系浏览器使用百度APP的ua时,会缺少搜索栏和控制栏。本脚本可以改善此情况。
当前为
// ==UserScript==
// @name 移动百度优化
// @namespace http://tampermonkey.net/
// @version 0.5
// @description 非百度系浏览器使用百度APP的ua时,会缺少搜索栏和控制栏。本脚本可以改善此情况。
// @author 那年那兔那些事
// @include *://m.baidu.com/s*
// @license MIT
// ==/UserScript==
(function() {
'use strict';
function getData(key) {
if (key === "ua") {
let ua = navigator.userAgent;
let isMobile = /mobile/i.test(ua);
let isBaidu = /baidu/i.test(ua);
return isMobile && isBaidu;
}
let ls = location.search;
if (key === "pd") {
return !/pd=/i.test(ls);
}
let searchWord = /word=/i.test(ls) ? ls.slice(ls.search("word=") + 5) : "";
searchWord = !searchWord&&/wd=/i.test(ls) ? ls.slice(ls.search("wd=") + 3) : searchWord;
searchWord = searchWord ? decodeURI(searchWord.split("&")[0]) : "";
let pageIndex = ls.search("pn=") !== -1 ? ls.slice(ls.search("pn=") + 3) : "";
pageIndex = pageIndex ? pageIndex.split("&")[0] : "";
pageIndex = pageIndex ? parseInt(1 + parseInt(pageIndex) / 10) : 1;
let homeUrl = "https://m.baidu.com/s?word=" + searchWord;
let lastUrl = homeUrl + ((pageIndex - 1) > 1 ? "&pn=" + (pageIndex - 2) * 10 : "");
let nextUrl = homeUrl + "&pn=" + pageIndex * 10;
let pcUrl = "https://www.baidu.com/s" + ls;
switch (key) {
case "ls":
return ls;
break;
case "word":
return searchWord;
break;
case "home":
return homeUrl;
break;
case "last":
return lastUrl;
break;
case "index":
return pageIndex;
break;
case "next":
return nextUrl;
break;
case "pc":
return pcUrl;
break;
default:
return [ls, searchWord, homeUrl, lastUrl, pageIndex, nextUrl, pcUrl];
break;
}
}
function createSearchBar() {
let extraSearchgBox = document.getElementsByClassName("searchboxtop newsearch-white-style")[0];
let tabLink = document.getElementsByClassName("se-head-tablink")[0];
if (!extraSearchgBox && tabLink) {
extraSearchgBox = document.createElement("div");
extraSearchgBox.setAttribute("m-service", "searchbox");
extraSearchgBox.setAttribute("class", "searchboxtop newsearch-white-style");
extraSearchgBox.setAttribute("style", "padding-top:10px");
extraSearchgBox.innerHTML =
"<form data-formposition='t' class='se-form' id='se-form' method='get' autocomplete='off' action='/s'><div><div class='suggest-back' style='display: none;'><i class='c-icon'></i></div><div class='con-wrap new-search-con'><div class='con-inner-left'><div class='se-bearicon'></div><input autocomplete='off' autocorrect='off' maxlength='64' id='kw' name='word' type='search' class='se-input adjust-input' placeholder='输入搜索词' value='" +
getData("word") +
"' data-sug='1'><div id='cross' class='cross' style='display: none;'></div></div><button id='se-bn' class='se-bn' type='submit'><span>百度一下</span></button></div></div></form><div class='searchboxtop-bg-fade'></div>";
tabLink.parentElement.insertBefore(extraSearchgBox, tabLink);
}
}
function createPageControl() {
let pageControl = document.getElementById("page-controller");
let pageFt = document.getElementById("page-ft");
if (!pageControl && pageFt) {
pageControl = document.createElement("div");
pageControl.id = "page-controller";
pageControl.style =
"position: relative;line-height: 52px;margin:.08rem 0;text-align: center;color: #9b9fa4";
pageControl.innerHTML =
"<div style='display:inline-block;width:20%;height:100%;'></div><div style='display:inline-block;width:20%;height:100%;'></div><div style='display:inline-block;width:20%;height:100%;'></div><div style='display:inline-block;width:20%;height:100%;'></div><div style='display:inline-block;width:20%;height:100%;'></div>";
pageControl.children[4].innerHTML = " PC ";
pageControl.children[3].innerHTML = " > ";
pageControl.children[2].innerHTML = "第 " + getData("index") + " 页";
pageControl.children[1].innerHTML = " < ";
pageControl.children[0].innerHTML = " << ";
pageControl.children[4].onclick = function() {
location.href = getData("pc");
}
pageControl.children[3].onclick = function() {
location.href = getData("next");
}
pageControl.children[2].onclick = function() {
let mgs = "请输入页码!\n注意:如果跳转的页码>实际页码上限,将显示第1页的内容";
let newIndex = prompt(msg);
newIndex = newIndex ? parseInt(newIndex.trim()) : "";
if (!newIndex) {
return false;
}
if (newIndex < 1) {
alert("页码必须为一个正整数");
} else {
let urlPn = newIndex > 1 ? "&pn=" + (newIndex - 1) * 10 : "";
location.href = getData("home") + urlPn;
}
}
pageControl.children[1].onclick = function() {
location.href = getData("last");
}
pageControl.children[0].onclick = function() {
location.href = getData("home");
}
pageFt.appendChild(pageControl);
document.getElementsByClassName("infinite-load-wrap")[0].style.display = "none";
}
}
if (getData("ua") && getData("pd")) {
var t = setTimeout(function() {
createSearchBar();
createPageControl();
}, 200);
setTimeout(function() {
clearInterval(t);
}, 2000);
}
})();