GitHub Inline Issue Link Bypass

Force normal navigation for GitHub issue and PR links that GitHub opens in preview modals.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub Inline Issue Link Bypass
// @namespace    https://github.com/
// @version      0.1.0
// @description  Force normal navigation for GitHub issue and PR links that GitHub opens in preview modals.
// @author       alexchexes
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @match        https://github.com/*
// @run-at       document-start
// @grant        none
// @license      MIT
// @contributionURL https://ko-fi.com/alexchexes
// @contributionURL https://github.com/sponsors/alexchexes
// ==/UserScript==

(() => {
  'use strict';

  const allowedHovercardTypes = new Set(['issue', 'pull_request']);
  const issuePathPattern = /^\/[^/?#]+\/[^/?#]+\/(?:issues|pull)\/\d+$/;

  function getAnchorFromEvent(event) {
    if (typeof event.composedPath === 'function') {
      for (const node of event.composedPath()) {
        if (node instanceof HTMLAnchorElement && node.href) {
          return node;
        }
      }
    }

    if (!(event.target instanceof Node)) {
      return null;
    }

    const element =
      event.target.nodeType === Node.ELEMENT_NODE
        ? event.target
        : event.target.parentElement;

    return element?.closest?.('a[href]') ?? null;
  }

  function getForcedNavigationHref(event, anchor) {
    if (event.button !== 0) {
      return null;
    }

    if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
      return null;
    }

    if (anchor.hasAttribute('download')) {
      return null;
    }

    if (anchor.target && anchor.target.toLowerCase() !== '_self') {
      return null;
    }

    if (!allowedHovercardTypes.has(anchor.dataset.hovercardType || '')) {
      return null;
    }

    const url = new URL(anchor.href, location.href);

    if (url.origin !== location.origin) {
      return null;
    }

    if (!issuePathPattern.test(url.pathname)) {
      return null;
    }

    return url.href;
  }

  window.addEventListener(
    'click',
    (event) => {
      const anchor = getAnchorFromEvent(event);

      if (!anchor) {
        return;
      }

      const href = getForcedNavigationHref(event, anchor);

      if (!href) {
        return;
      }

      event.preventDefault();
      event.stopImmediatePropagation();
      location.assign(href);
    },
    { capture: true, passive: false }
  );
})();