Anti BD Redirect

反重定向

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);