Greasy Fork is available in English.

百度搜索优化sp

Tampermonkey脚本,屏蔽百度推广和各种广告+页面样式美化+url重定向+屏蔽域名

// ==UserScript==
// @name         百度搜索优化sp
// @namespace    bdso_sp
// @description  Tampermonkey脚本,屏蔽百度推广和各种广告+页面样式美化+url重定向+屏蔽域名
// @update-url   https://greasyfork.org/zh-CN/users/218062-vizo
// @author       vizo
// @version      2.7.9
// @require      https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @include      *://www.baidu.com/*
// @run-at       document-start
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @connect      *
// ==/UserScript==

'use strict'

let bdcss = `
body,
div,
th,
td,
.p1,
.p2,
h1,
h2,
h3,
h4,
h5,
h6,
a {
  font-family: sans-serif,"Helvetica Neue",Helvetica,"PingFang SC","Microsoft YaHei","Hiragino Sans GB",Arial;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  font-weight: normal !important;
}
.c-tools,
.to_tieba,
.res_top_banner,
.op-soft-normal-tip,
.hint_right_middle,
.rrecom-container,
.hint_common_restop,
.rrecom-btn-parent,
.c-pingjia,
.ad-block,
#m2c69bdd0,
#imsg,
#rs_top_new,
#foot,
#page .fk,
#content_left .leftBlock,
#content_left > table,
#content_right .c-gray,
#content_right .hint_right_top,
#content_right .opr-toplist-info,
#content_right table table {
  display: none !important;
}
a.m {
  box-shadow: -9px 0px 0 0px #fff;
  color: #fff;
  pointer-events: none;
}
.container_l {
  width: 100%;
}
#wrapper.wrapper_l {
  overflow: hidden;
}
#head .headBlock {
  margin: 4px 0 10px 0 !important;
}
#head .headBlock a:hover {
  text-decoration: underline !important;
}
#head .headBlock .hint_toprq_tips {
  width: 730px;
  margin: 0 auto;
}
#form {
  float: none !important;
  margin: 0 auto !important;
  width: 740px;
  display: block;
  padding-top: 16px;
  padding-left: 0;
  transform: translateX(-6px);
}
#form .s_ipt_wr {
  width: 616px !important;
  height: 36px;
  border: 1px solid #c4c7ce;
  border-radius: 0;
}
#form .s_btn {
  height: 38px !important;
  line-height: 40px;
  font-size: 16px;
  display: block;
  border-radius: 0;
  background-color: #07e;
}

#head.fix-head #form {
  padding-top: 8px;
}
#head.fix-head #form #xyb_goo_so_btn {
  top: 16px;
}
#head.fix-head #form .bdsug-new {
  top: 49px;
}

.soutu-env-result .soutu-layer,
.soutu-env-imgresult .soutu-layer {
  width: 725px !important;
}
.soutu-layer .soutu-url-btn {
  width: 186px !important;
}
.wrapper_l #kw {
  height: 36px;
  font: 16px/36px Microsoft YaHei;
  width: 570px !important;
  margin: 0 !important;
  padding-left: 7px;
}
.content_none {
  width: 725px;
  padding: 40px 0;
  margin: 0 auto;
}
.search_tool_conter {
  margin: 0 auto !important;
}
.nums {
  margin: 0 auto !important;
}
#s_tab {
  width: 717px;
  margin: 0 auto;
  position: relative;
  padding: 72px 0 0 0 !important;
  overflow: visible !important;
  border-bottom: none !important;
  opacity: 0;
  animation: fadeIn 1s both;
}
.s_form::after,
.s_tab::after {
  visibility: visible !important;
}
#s_tab::after {
  content: '';
  width: 5000px;
  height: 1px;
  background: #e1e1e1;
  position: absolute;
  left: -2500px;
  bottom: -2px;
  z-index: 3;
}
.head_nums_cont_inner {
  overflow-x: hidden !important;
  opacity: 0;
  animation: fadeIn 1s both;
}

#content_right {
  display: none !important;
}

body {
  background: #f8f8f8;
  background-attachment: fixed;
}
a {
  text-decoration: none !important;
  color: #07e;
}
a:hover {
  color: #07e;
}
a em {
  text-decoration: none;
}

#u,
#head,
#tool,
#search,
#foot {
  font-size: 14px;
}
#form .s_ipt_wr {
  width: 625px;
}

body .wrapper_new #form .bdsug-new {
  width: 615px;
  border: 1px solid #c4c5c6 !important;
  border-radius: 0;
  top: 57px;
}
.wrapper_new #form .bdsug-new ul {
  border-top: 0 !important;
}
#s_tab b {
  border-bottom: 3px #07e solid;
  color: #07e;
}

#rs {
  width: 690px;
  padding: 10px 20px 10px;
  margin: 10px auto 10px auto !important;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 0 5px #dbdbdb;
  opacity: 0;
  overflow: hidden;
  animation: fadeInUp 0.35s both;
}
#rs a:hover {
  text-decoration: underline !important;
}
#rs .tt {
  border-bottom: 1px solid #ddd;
}
#rs th {
  width: 300px;
  font-size: 15px;
  padding-top: 5px;
  padding-left: 50px;
}
#container {
  margin: 0 auto !important;
}
#content_left {
  width: 725px !important;
}
#content_left td a:hover,
#content_left .c-row a:hover {
  text-decoration: underline !important;
}
#wrapper_wrapper #content_left {
  width: 730px;
  padding-left: 0;
  float: none;
  margin: 0 auto;
  opacity: 0;
  animation: fadeInUp 0.35s both;
}
#content_left .c-container {
  width: 100% !important;
  box-sizing: border-box;
  margin-bottom: 10px !important;
  padding: 10px 15px 10px 15px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 0 5px #c4c5c6;
}
#content_left article[class*="open-source-software-card_"],
#content_left article[class*="aladdin_"] {
  padding: 0;
  box-shadow: none;
}
#content_left .c-container h3 {
  padding-bottom: 5px;
  margin-bottom: 8px;
  font-weight: bold;
  border-bottom: 1px solid #ddd;
}
.c-container h3 a {
  position: relative;
  display: inline-block;
  font-size: 18px;
  font-family: Helvetica Neue, sans-serif, Microsoft YaHei, helvetica, arial;
}
#content_left .c-container h3 a.favurl {
  background-position: 0 5px;
  background-repeat: no-repeat;
}
#content_left .OP_LOG_LINK.c-text {
  font-size: 14px !important;
  vertical-align: -5px;
}
#content_left .OP_LOG_LINK.c-text:visited {
  color: #fff;
}
#content_left .OP_LOG_LINK.c-text:hover {
  color: #fff;
}
#content_left .c-container h3 a:hover {
  color: #07e;
}
#content_left .c-container h3 a:hover:after {
  left: 0;
  width: 100%;
  transition: width 0.35s;
}
#content_left .c-container h3 a:after {
  content: '';
  position: absolute;
  border-bottom: 2px solid #37e;
  bottom: -4px;
  left: 100%;
  width: 0;
  transition: width 0.35s, left 0.35s;
}

#content_left .c-container h3 a {
  position: relative;
}
#content_left .c-container h3 a:visited {
  color: #660099;
}
#content_left .c-container h3 a:visited:hover:after {
  left: 0;
  width: 100%;
  transition: width 0.35s;
}
#content_left .c-container h3 a:visited:after {
  content: '';
  position: absolute;
  border-bottom: 2px solid #660099;
  bottom: -2px;
  left: 100%;
  width: 0;
  transition: width 0.35s, left 0.35s;
}

#content_left .c-abstract {
  width: 100%;
}
#content_left .c-span18 {
  width: 560px;
}
#content_left .c-border {
  width: auto;
}

#content_left .op-soft-btnbox .c-btn-primary {
  display: none;
}
#content_left .op-soft-btnbox a {
  border: 1px solid;
  padding: 7px;
}

#content_left .op-soft-btnbox a:hover {
  text-decoration: none !important;
  background: #2c99ff;
  color: #fff;
}
#content_left .c-container .f13,
#content_left .c-container .c-row,
.c-abstract {
  font-size: 14px;
  line-height: 24px;
}
.c-border {
  width: 518px;
  padding: 9px;
  margin: auto;
  border: 1px solid #f3f3f3;
  border-bottom-color: #f3f3f3;
  border-right-color: #f3f3f3;
  box-shadow: 1px 2px 0px rgba(0, 0, 0, 0);
}
@keyframes fadeInUp {
  from {
    opacity: 0;
    -webkit-transform: translateY(30px);
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    -webkit-transform: none;
    transform: none;
  }
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

#page {
  display: block;
  width: 710px;
  height: 40px;
  line-height: 40px;
  padding-left: 0px;
  padding: 5px 5px 5px 15px;
  border-radius: 3px;
  margin: 30px auto 80px auto !important;
  text-align: center;
}
#page a,
#page strong {
  height: auto;
  box-shadow: 0 0 10px #c4c5c6;
}
#page .pc:hover {
  background: #07e;
  color: #07e;
}
#page strong .pc {
  background: #07e;
  color: #fff;
}
#page strong .pc:hover {
  background: #07e;
  color: #fff;
}
#page .page-inner {
  padding: 0 !important;
}

#con-at .result-op {
  margin: 10px auto auto 10px;
  padding: 5px 40px 5px 20px;
  background: #fdfdfd;
  box-shadow: 0 0px 1px 0.5px #0f93d2, 0 1px 5px 0px rgba(0, 0, 0, 0.7);
  transition: 0.4s;
}

body > .result-op {
  background: none !important;
}

#content_left > div:not([id]) {
  width: 640px;
  margin-bottom: 10px;
  padding: 10px 45px;
  border-radius: 2px;
  box-shadow: 0 0 5px #c4c5c6;
}
.t > div:not([id]) {
  width: 640px;
  margin-bottom: 10px;
  padding: 10px 45px;
  border-radius: 3px;
  box-shadow: 0 0 10px #c4c5c6;
}

body > div[style*='position: absolute;'],
body > div[style*='position: fixed;'] {
  display: none !important;
}

#content_left > div[style*='display:block !important'] {
  position: absolute !important;
  top: -5000px !important;
  display: none !important;
}

.bdpage-l,
.bdpage-r {
  width: 300px;
  height: 500px;
  overflow: hidden;
  cursor: pointer;
  position: fixed;
  top: 0;
  bottom: 0;
  margin: auto;
}
.bdpage-l.disa,
.bdpage-r.disa {
  cursor: not-allowed;
}
.bdpage-l:hover,
.bdpage-r:hover {
  background: rgba(100, 100, 100, 0.03);
}
.bdpage-l::before,
.bdpage-r::before {
  content: '';
  width: 100px;
  height: 100px;
  border-left: 2px solid #e7e9eb;
  border-bottom: 2px solid #e7e9eb;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: auto;
}
.bdpage-l:hover::before,
.bdpage-r:hover::before {
  border-color: #fff;
}
.bdpage-l {
  left: 0;
}
.bdpage-l::before {
  left: 45%;
  transform: rotate(45deg);
}
.bdpage-r {
  right: 0;
}
.bdpage-r::before {
  right: 45%;
  transform: rotate(225deg);
}
#xyb_goo_so_btn {
  font-size: 15px;
  cursor: pointer;
  position: absolute;
  top: 24px;
  right: -68px;
  z-index: 5;
}
@media (max-width: 1280px) {
  .bdpage-l,
  .bdpage-r {
    width: 150px;
  }
}

`

GM_addStyle(bdcss)

//导入CSS

function importCSS() {
  
  let isAddStyle = !Array.from(document.querySelectorAll('style')).some(v => {
    return /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(v.id)
  })
  
  if (isAddStyle) {
    GM_addStyle(bdcss)
  }
  
  let gooSo = $('body').find('#xyb_goo_so_btn').length
  if (!gooSo) {
    $('body').find('#form').append('<a id="xyb_goo_so_btn" href="javascript:;">谷歌搜索</a>')
  }
}

$(function() {
  
  init()

  try {
    let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
    let observer = new MutationObserver(mutationfunc)
    let wrapper = document.querySelector('#wrapper_wrapper')
    //动态监视DOM树的变化
    observer.observe(wrapper, {
      attributes: true,
      childList: true,
      subtree: true,
    })
  } catch (e) {}

  // 动态加载函数
  function mutationfunc() {
    init()
  }

  function init() {
    importCSS()
    removeADS()
    cdxUrl()
    appendElem()
    setPNStatus()
  }

  // 设置a标签真实地址
  function cdxUrl() {
    // 忽略解析的域名列表
    let ignore_list = ['segmentfault.com']
    
    $('#content_left .c-container .t > a').each(function() {
      let that = $(this)
      let url = that.attr('href')
      let u_txt = that.closest('.result').find('.f13 > .c-showurl').text()
      let dms = u_txt.match(/\b[\w]+\.[a-z]+(?=\/|$)/)+''
      let cdx = that.hasClass('cdx_ed')

      if (!cdx && !ignore_list.includes(dms)) {
        GM_xmlhttpRequest({
          url: url,
          method: 'head',
          onload: function(xhr) {
            try {
              that.attr('href', xhr.finalUrl).addClass('cdx_ed')
            } catch (e) {}
          },
        })
      }
    })
  }

  // 上一页下一页按钮
  function appendElem() {
    let len = $('body').find('.bdpage-l').length
    if (!len) {
      $('body').append('<div class="bdpage-l"></div><div class="bdpage-r"></div>')
    }
  }

  // 设置上下页状态
  function setPNStatus() {
    let fkl = true
    let fkr = true
    $('#page .n').each(function() {
      let that = $(this)
      let text = that.text()
      if (~text.indexOf('上一页')) {
        fkl = false
      }
      if (~text.indexOf('下一页')) {
        fkr = false
      }
    })
    if (fkl) {
      $('body')
        .find('.bdpage-l')
        .addClass('disa')
    } else {
      $('body')
        .find('.bdpage-l')
        .removeClass('disa')
    }
    if (fkr) {
      $('body')
        .find('.bdpage-r')
        .addClass('disa')
    } else {
      $('body')
        .find('.bdpage-r')
        .removeClass('disa')
    }
  }

  //屏蔽广告和推广
  function removeADS() {
    let $ads = ['#content_left>div[style*="display:block !important"]', '#content_left>div:not([id])', '#content_left>#clone']
    let $selctor = $($ads.join())
    $selctor.remove()

    $('#content_left .result[id=1]').each(function() {
      let tis = $(this)
      let txt = tis.find('.f13 .m').text()
      if (txt == '广告') {
        tis.remove()
      }
    })
  }
  
  // 空格键按下快速搜索
  $(document).on('keydown', function(e) {
    let isFocus = $('#kw').is(':focus')
    if (!isFocus && e.which === 32) {
      $('#kw')
        .focus()
        .select()
      return false
    }
  })
  $('body').on('click', '.bdpage-l', function() {
    $('#page .n').each(function() {
      let that = $(this)
      let text = that.text()
      if (~text.indexOf('上一页')) {
        that[0].click()
      }
    })
  })
  $('body').on('click', '.bdpage-r', function() {
    $('#page .n').each(function() {
      let that = $(this)
      let text = that.text()
      if (~text.indexOf('下一页')) {
        that[0].click()
      }
    })
  })
  
  // 点击谷歌搜索
  $('body').on('mousedown', '#xyb_goo_so_btn', function(e) {
    e.preventDefault()
    if (e.button === 0 || e.button === 1) {
      let goo = 'https://www.google.com.hk/search?q='
      let wd = $('body').find('#kw').val()
      window.open(goo + wd, '_blank').location
    }
  })
  
})