Reddit force local search

Force search in subreddit by default

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==

// @name        Reddit force local search

// @description Force search in subreddit by default

// @namespace   https://www.reddit.com/

// @match       https://www.reddit.com/*

// @grant       none

// @version     1.0

// @author      r/KaKi_87

// ==/UserScript==

const form = document.querySelector('#SearchDropdown form');

const getInput = () => document.querySelector('#header-search-bar');

const getSubreddit = () => {
  
  const path = window.location.href.split('/');
  
  return path[3] === 'r' ? path[4] : undefined;
  
};

const handler = event => {
  
  const
  
    query = getInput().value,
        
    subreddit = getSubreddit();
  
  if(!subreddit) return;
  
  event.stopPropagation();

  event.stopImmediatePropagation();

  event.preventDefault();

  window.location.href = `https://www.reddit.com/r/${subreddit}/search?q=${query}&restrict_sr=1`;
  
};

form.addEventListener('submit', handler, true);

const loop = () => {
  
  const subreddit = getSubreddit();
  
  const input = getInput();
  
  input.setAttribute('placeholder', subreddit ? `Search in r/${subreddit}` : 'Search');
  
  setTimeout(loop, 250);
  
};

loop();