Này người xa lạ!

Còn chờ gì nữa mà không mau đăng nhập hoặc đăng ký để cùng tham gia thảo luận với cộng đồng!

Amazon always sort by price

đã sửa April 7 trong Phát triển [?]

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?

Bình luận

  • 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}));
    
  • 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?

Đăng nhập hoặc Đăng ký để gửi bình luận.