Greasy Fork

Greasy Fork is available in English.

Kbin Open Posts in New Tab

Automatically add target="_blank" to all links embedded in h2 tags on Kbin.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Kbin Open Posts in New Tab
// @match       https://kbin.social/*
// @match       https://fedia.io/*
// @match       https://karab.in/*
// @version      0.1
// @description  Automatically add target="_blank" to all links embedded in h2 tags on Kbin.
// @namespace    http://greasyfork.icu/en/users/1127287-harasho
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  function addTargetBlankToLinks() {
    const articleHeaders = document.querySelectorAll("article header"); // Get all header tags within article tags

    // Loop through each header tag and find h2 tags with links
    articleHeaders.forEach(header => {
      const h2Tags = header.querySelectorAll("h2"); // Get all h2 tags within the header

      // Loop through h2 tags and find links
      h2Tags.forEach(h2Tag => {
        const links = h2Tag.querySelectorAll("a"); // Get all links inside the h2 tag

        // Loop through links and add target="_blank" if not already present
        links.forEach(link => {
          if (!link.hasAttribute("target")) {
            link.setAttribute("target", "_blank");
          }
        });
      });
    });
  }

  // Run the function initially on page load
  addTargetBlankToLinks();

  // Use MutationObserver to handle dynamic changes to the page content
  const observer = new MutationObserver(() => {
    addTargetBlankToLinks();
  });

  // Observe changes to the entire document's subtree
  observer.observe(document.documentElement, {
    childList: true,
    subtree: true
  });
})();