Avoid Yandex Turbo

Redirect directly to target page avoiding Yandex Turbo

目前為 2020-11-19 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Avoid Yandex Turbo
// @name:ru Обход Яндекс Турбо
// @description Redirect directly to target page avoiding Yandex Turbo
// @description:ru Переадресация на целевую страницу в обход Яндекс Турбо
// @author Autapomorph
// @version 2.0.0
// @run-at document_start
// @match *://yandex.ru/*
// @match *://*.turbopages.org/*
// @supportURL https://github.com/Autapomorph/userscripts/issues
// @license MIT
// @namespace https://greasyfork.org/users/689919
// ==/UserScript==

(function () {
  function redirectWithTurboOverlay() {
    var titleHostActive = $('.turbo-overlay__title-host_active');
    if (!titleHostActive || !titleHostActive.length) {
      return;
    }

    
    var titleHostActiveText = titleHostActive[0].textContent;
    if (!titleHostActiveText.length) {
      return;
    }

    var hostLinks = $('a[data-sc-host]');
    for (var i = 0; i < hostLinks.length; i += 1) {
      var hostLink = $(hostLinks[i]);
      
      var dataCounter;
      try {
        dataCounter = JSON.parse(hostLink.attr('data-counter'));
      } catch (error) {
        return;
      }

      if (dataCounter.find(e => e.indexOf(titleHostActiveText) > -1)) {
        var redirect;
        if (dataCounter[0] === 'b') {
          redirect = dataCounter[1];
        } else if (dataCounter[0] === 'w') {
          redirect = dataCounter[3];
        } else {
          return;
        }
        
        top.location.replace(redirect);
      }
    }

    $('a[data-sc-host]').each(function () {
      var dataCounterAttr = $(this).attr('data-counter');
      if (!dataCounterAttr) return;

      var dataCounter = JSON.parse(dataCounterAttr);
      if (dataCounter.find(e => e.indexOf(titleHostActiveText) > -1)) {
        var redirect;
        if (dataCounter[0] === 'b') {
          redirect = dataCounter[1];
        } else if (dataCounter[0] === 'w') {
          redirect = dataCounter[3];
        }
        top.location.replace(redirect);
      }
    });
  }

  function redirectWithURL() {
    var urlPathname = top.location.pathname;
    var turboIndex = urlPathname.indexOf('/turbo/');
    var delimeterIndex = urlPathname.search(/\/(s|h)/);
    var delimeterLength = 2;

    if (delimeterIndex < 0) {
      return;
    }
    
    var host =
      turboIndex === -1
        ? urlPathname.substring(1, delimeterIndex)
        : urlPathname.substring(turboIndex + '/turbo/'.length, delimeterIndex);
    var pathName = urlPathname.substring(delimeterIndex + delimeterLength);
    top.location.replace('//' + host + pathName);
  }

  var urlPathname = top.location.pathname;
  if (/\.*\/(s|h)\/.*/.test(urlPathname)) {
    redirectWithTurboOverlay();
    redirectWithURL();
  }
})();