Greasy Fork

Greasy Fork is available in English.

Avoid Yandex Turbo

Redirect directly to target page avoiding Yandex Turbo

当前为 2021-04-25 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(function avoidYandexTurbo() {
  function redirectWithTurboOverlay() {
    const titleHostActive = document.querySelector('.turbo-overlay__title-host_active');
    if (!titleHostActive) return;

    const titleHostActiveText = titleHostActive.textContent;
    const hostLinks = document.querySelectorAll('a[data-sc-host]');
    for (let i = 0; i < hostLinks.length; i += 1) {
      const hostLink = hostLinks[i];
      let dataCounter;
      try {
        dataCounter = JSON.parse(hostLink.getAttribute('data-counter'));
      } catch (error) {
        return;
      }

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

        top.location.replace(redirect);
      }
    }
  }

  function redirectWithURLPathname(urlPathname) {
    const turboIndex = urlPathname.indexOf('/turbo/');
    const delimeterIndex = urlPathname.search(/\/(s|h)\//);
    const delimeterLength = 2;

    if (delimeterIndex < 0) return;

    const host =
      turboIndex === -1
        ? urlPathname.substring(1, delimeterIndex)
        : urlPathname.substring(turboIndex + '/turbo/'.length, delimeterIndex);
    const pathName = urlPathname.substring(delimeterIndex + delimeterLength);
    top.location.replace(`//${host}${pathName}`);
  }

  function redirectWithURLSearchParam(urlSearchParams) {
    const textQuery = urlSearchParams.get('text');
    if (textQuery) {
      top.location.replace(textQuery);
    }
  }

  function isTurboPage(urlHostname, urlPathname, urlSearchParams) {
    // turbopages.org
    if (/\.turbopages.org/.test(urlHostname)) return true;

    // yandex.ru
    if (/yandex.ru/.test(urlHostname) && urlPathname.includes('/turbo')) {
      if (/\.*\/(s|h)\/.*/.test(urlPathname) || urlSearchParams.has('text')) {
        return true;
      }
    }

    return false;
  }

  function main() {
    const urlHostname = top.location.hostname;
    const urlPathname = top.location.pathname;
    const urlSearchParams = new URLSearchParams(top.location.search);

    if (!isTurboPage(urlHostname, urlPathname, urlSearchParams)) return;
    redirectWithTurboOverlay();
    redirectWithURLPathname(urlPathname);
    redirectWithURLSearchParam(urlSearchParams);
  }

  if (typeof module === 'object' && module.exports) {
    module.exports = {
      avoidYandexTurbo,
      main,
      isTurboPage,
      redirectWithTurboOverlay,
      redirectWithURLPathname,
      redirectWithURLSearchParam,
    };
    return;
  }

  let currentURLPathname = top.location.pathname;
  setInterval(() => {
    if (currentURLPathname !== top.location.pathname) {
      currentURLPathname = top.location.pathname;
      main();
    }
  }, 1000);

  main();
})();