Rewrite reddit links to use the Narwhal 2 URI scheme

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

目前為 2025-04-02 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();