🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Amazon Auto-Sort by Best Sellers

Automatically detects and immediately redirects Amazon search results to the 'Best Sellers' sort order, bypassing the default 'Featured' sort and eliminating page double-loads.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Amazon Auto-Sort by Best Sellers
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Automatically detects and immediately redirects Amazon search results to the 'Best Sellers' sort order, bypassing the default 'Featured' sort and eliminating page double-loads.
// @author       Gemini
// @match        *://*.amazon.com/s*
// @match        *://*.amazon.co.uk/s*
// @match        *://*.amazon.de/s*
// @match        *://*.amazon.ca/s*
// @match        *://*.amazon.fr/s*
// @match        *://*.amazon.it/s*
// @match        *://*.amazon.es/s*
// @match        *://*.amazon.co.jp/s*
// @match        *://*.amazon.com.au/s*
// @run-at       document-start
// @grant        none
// @license      CC BY 4.0
// ==/UserScript==

/*
 * This work is licensed under the Creative Commons Attribution 4.0 International License.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/
 * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
 */

(function() {
    // Check if the current page is an Amazon search results page (URL path starts with '/s')
    if (window.location.pathname.startsWith('/s')) {
        'use strict';

        // --- Configuration ---
        // The value attribute for the "Best Sellers" sort order, confirmed to be correct for direct URL manipulation.
        const BEST_SELLERS_SORT_VALUE = 'exact-aware-popularity-rank';
        // ---------------------

        const url = new URL(window.location.href);
        const urlParams = url.searchParams;
        const currentSort = urlParams.get('s');

        console.log(`TAMPERMONKEY DEBUG: Current URL is an Amazon search page: ${window.location.href}`);
        console.log(`TAMPERMONKEY DEBUG: Current 's' URL parameter is: ${currentSort}`);

        // 1. Check if the URL already contains the correct sort value
        if (currentSort === BEST_SELLERS_SORT_VALUE) {
             console.log(`TAMPERMONKEY DEBUG: Sort is already set to target value '${BEST_SELLERS_SORT_VALUE}'. No action needed.`);
             return; // Stop execution if already sorted
        }

        // 2. Immediate Redirection: Construct the new URL from existing parameters

        // Preserve all existing query parameters but force the 's' parameter to our target value.
        urlParams.set('s', BEST_SELLERS_SORT_VALUE);

        // Amazon sometimes adds a 'ref' parameter that points to the previous sort state.
        // We replace it with the known 'Best Sellers' reference for a clean navigation.
        const knownBestSellersRef = 'sr_st_exact-aware-popularity-rank';
        if (urlParams.has('ref')) {
            // Check if the existing ref already contains a sort reference we should replace
            const refValue = urlParams.get('ref');
            if (refValue.startsWith('sr_st_')) {
                 urlParams.set('ref', knownBestSellersRef);
            } else {
                 // For general search entry points, we can append the sort ref cleanly
                 urlParams.set('ref', knownBestSellersRef);
            }
        } else {
             // If no 'ref' exists, add the Best Sellers ref
             urlParams.set('ref', knownBestSellersRef);
        }

        // Update the full URL string
        const newUrl = url.toString();

        console.log(`Amazon Auto-Sort: Current sort is '${currentSort}'. Forcing immediate redirect to Best Sellers.`);
        console.log(`TAMPERMONKEY DEBUG: Redirecting to: ${newUrl}`);

        // Perform the direct navigation, preventing the initial page from loading.
        window.location.replace(newUrl);
    }
})();