Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

2019-12-11 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Greasy Fork - Change Default Script Sort
// @namespace    https://greasyfork.org/users/649
// @version      1.3.4
// @description  Change default script sort on GreasyFork
// @author       Adrien Pyke
// @match        *://greasyfork.org/*/users/*
// @match        *://greasyfork.org/*/scripts*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @require      https://gitcdn.link/repo/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
// @require      https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
// @run-at       document-start
// ==/UserScript==

(() => {
  'use strict';

  const commonValues = [
    { value: 'daily-installs', text: 'Daily installs' },
    { value: 'total_installs', text: 'Total installs' },
    { value: 'ratings', text: 'Ratings' },
    { value: 'created', text: 'Created date' },
    { value: 'updated', text: 'Updated date' },
    { value: 'name', text: 'Name' }
  ];
  const Config = GM_config([
    {
      key: 'all',
      label: 'All Scripts Sort',
      default: 'daily-installs',
      type: 'dropdown',
      values: commonValues
    },
    {
      key: 'search',
      label: 'Search Sort',
      default: 'relevance',
      type: 'dropdown',
      values: [{ value: 'relevance', text: 'Relevance' }, ...commonValues]
    },
    {
      key: 'user',
      label: 'User Profile Sort',
      default: 'daily-installs',
      type: 'dropdown',
      values: commonValues
    }
  ]);
  GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);

  const onSearch = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?.*\?.*q=/iu);
  const onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?/iu);
  const onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/users\//iu);

  waitForElems({
    sel: '#script-list-sort > ul > li:first-of-type > a',
    stop: true,
    onmatch(defaultSort) {
      const url = new URL(defaultSort.href);
      url.searchParams.set('sort', onSearch ? 'relevance' : 'daily-installs');
      defaultSort.href = url.href;
    }
  });

  const url = new URL(location.href);
  const sort = url.searchParams.get('sort');
  if (!sort) {
    const cfg = Config.load();
    let cfgSort;
    if (onSearch) {
      cfgSort = cfg.search;
    } else if (onScripts) {
      cfgSort = cfg.all;
    } else if (onProfile) {
      cfgSort = cfg.user;
    }
    if (cfgSort) {
      url.searchParams.set('sort', cfgSort);
      window.location.replace(url.href);
    }
  }
})();