Greasy Fork 还支持 简体中文。

Rewrite reddit links to use the Narwhal 2 URI scheme

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

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

// ==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_addStyle
// @unwrap
// @version     1.0
// @author      HealsCodes
// @description 4/1/2025, 12:09:44 PM
// @license     GPL3
// ==/UserScript==

function updatePageLinks(element) {
    'use strict';

    // 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 ['https://reddit.com', 'https://m.reddit.com', 'https://www.reddit.com', 'https://reddit.app.link']) {
          if (originalUrl.startsWith(prefix)) {
            var encodedUrl = encodeURIComponent(originalUrl);
            Object.assign(link, {
              textContent: originalTextContent + ' ' + String.fromCodePoint(0x1f433),
              href: 'narwhal://open-url/' + encodedUrl
            });
            return;
          }
        }
    }

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

(function () {
  // run on ajax updates
  waitForKeyElements(`a[href*="reddit."]`, (element) => updatePageLinks(), false);
  // run on initial page load
  document.updatePageLinks(undefined);
})();