Discussions » Development

Amazon always sort by price

§
Posted: 2019-04-06
Edited: 2019-04-07

Amazon always sort by price

This should be an easy one, but I'm not experienced enough.

document.getElementById("s-result-sort-select").selectedIndex = 2;

does change it in the dropdown, but doesn't trigger the event listener on the surrounding form.

Redirecting (add URL parameter) also doesn't work (because of CSP?):

// ==UserScript==
// @name         Amazon sort by price
// @namespace    graphen
// @version      1.0.0
// @description  Automatically switch to sorting by price (ascending)
// @include      /^https?:\/\/www\.amazon\.(cn|in|co\.jp|sg|fr|de|it|nl|es|co\.uk|ca|com(\.(mx|au|br))?)\/s\?.*$/
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */
(function() {
    'use strict';

    //check if already sorted
    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    var sort = getUrlVars()["s"];

    if (!sort) {
        var forward = window.location.href + "&s=price-asc-rank";
        window.location.href.replace(forward);
    }
})();

Something like

document.getElementById("s-result-sort-select").children[1].click();

also doesn't do the job.

Can someone please help?

woxxomMod
§
Posted: 2019-04-06

Direct property modification usually needs a change event being dispatched to notify the page scripts:

document.getElementById("s-result-sort-select").dispatchEvent(new Event('change', {bubbles: true}));
§
Posted: 2019-04-07

Thank you wOxxOm, that's a good info to remember.

Unfortunately changing the dropdown select and triggering the event change is not enough.

document.getElementById("s-result-sort-select").selectedIndex = 2;
document.getElementById("s-result-sort-select").dispatchEvent(new Event('change', {bubbles: true}));

Shouldn't that work or am I thinking too simple?

Post reply

Sign in to post a reply.