Anti BD Redirect

反重定向

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Anti BD Redirect
// @namespace   [email protected]
// @author      [email protected]
// @description 反重定向
// @include     https://www.baidu.com/s*
// @include     https://www.baidu.com/baidu*
// @version     0.1
// @grant       GM_xmlhttpRequest
// ==/UserScript==

function checkUrl(url){
  var headUrls = ['http://www.baidu.com/link?url=',]
  for(var i=0;i<headUrls.length;i++){
    if(url.startsWith(headUrls[i])){
      return true
    }
  }
}

function setTrueUrl(element) {
  var url = element.getAttribute('href')
  GM_xmlhttpRequest({
    url: url,
    method: 'HEAD',
    onload: function (response) {
      console.log(response)
      element.setAttribute('href', response.finalUrl)
    }
  })
}
function main(al) {
  for (var i = 0; i < al.length; i++) {
    var href = al[i].getAttribute('href')
    if (href && checkUrl(href)) {
      (function (a) {
        setTrueUrl(a)
      }) (al[i])
    }
  }
}
function listener(e) {
  var al = document.querySelectorAll('#container a')
  main(al)
}
listener()
var target = document.querySelector('#container');
 
// 创建观察者对象
var observer = new MutationObserver(function(mutations) {
  listener()
  //mutations.forEach(function(mutation) {});    
});
 
// 配置观察选项:
var config = {'childList': true, 'subtree': true}
 
// 传入目标节点和观察选项
observer.observe( document.querySelector('#wrapper_wrapper'), config);