Rewrite reddit links to use the Narwhal 2 URI scheme

4/1/2025, 12:09:44 PM

当前为 2025-04-02 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Rewrite reddit links to use the Narwhal 2 URI scheme
// @namespace   codes.heals.violentmonkey.narwhal2-opener
// @match       https://*/*
// @require     https://update.greasyfork.org/scripts/446257/1059316/waitForKeyElements%20utility%20function.js
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// @grant       GM_unregisterMenuCommand
// @grant       GM_addValueChangeListener
// @run-at      document-end
// @version     1.2
// @author      HealsCodes
// @description 4/1/2025, 12:09:44 PM
// @license     GPL3
// ==/UserScript==

const redditLinkDomains = ['https://reddit.com', 'https://m.reddit.com', 'https://www.reddit.com', 'https://reddit.app.link'];

function updatePageLinks(element) {
    'use strict';

    function updateTextContent(textContent) {
      if (GM_getValue('addWhaleMark', true)) {
        return textContent + ' ' + String.fromCodePoint(0x1f433);
      }
      return textContent;
    }

    // Function to extract the original URL and redirect to Narwhal
    function redirectToNarwhal(link) {
        var originalUrl = link.href;
        var originalTextContent = link.textContent;

        if (originalUrl.startsWith('narwhal://')) {
          // already processed
          return;
        }

        for(const prefix of redditLinkDomains) {
          if (originalUrl.startsWith(prefix)) {
            let encodedUrl = encodeURIComponent(originalUrl);
            let textContent = updateTextContent(originalTextContent);
            Object.assign(link, {
              textContent: textContent,
              href: 'narwhal://open-url/' + encodedUrl
            });
            return;
          }
        }
    }

    if (element !== undefined) {
      redirectToNarwhal(element);
    } else {
      document.querySelectorAll(`a[href*="reddit."]`).forEach(link => redirectToNarwhal(link));
    }
}

function menuCommandTitle(state) {
  let checked = (state != null) ? state : GM_getValue('addWhaleMark', true);
  return String.fromCodePoint(checked ? 0x2611 : 0x2610) + ' Add ' + String.fromCodePoint(0x1f433) + ' to Narwhal links';
}

let valueChangedListener = GM_addValueChangeListener('addWhaleMark', function(name, oldVal, newVal, remote) {
  GM_unregisterMenuCommand(menuCommandTitle(oldVal));
  GM_registerMenuCommand(menuCommandTitle(newVal), (_) => GM_setValue('addWhaleMark', !GM_getValue('addWhaleMark', true)));
});


(function () {
  // register the context menu handler
  GM_registerMenuCommand(menuCommandTitle(null), (_) => GM_setValue('addWhaleMark', !GM_getValue('addWhaleMark', true)));
  // run on ajax updates
  waitForKeyElements(`a[href*="reddit."]`, (element) => updatePageLinks(), false);
  // run on initial page load
  document.updatePageLinks(undefined);
})();