🏠 Home 

Greasy Fork is available in English.

Amazon Enhancements: Ratings Display and Filtering

Display rating scores and add filtering capabilities on Amazon search r###lts.


安装此脚本?
// ==UserScript==
// @name Amazon Enhancements: Ratings Display and Filtering
// @namespace http://tampermonkey.net/
// @version 2.3
// @description Display rating scores and add filtering capabilities on Amazon search r###lts.
// @author Dave w/ Claudi.ai & ChatGPT
// @match https://*.amazon.com/s?*
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let currentFilter = 0;
// Function to enhance rating display
const enhanceRatingDisplay = (r###lt) => {
const ratingElement = r###lt.querySelector('i.a-icon-star-small span.a-icon-alt');
if (ratingElement && !r###lt.classList.contains('enhanced-rating')) {
const ratingText = ratingElement.textContent.trim();
const ratingValue = parseFloat(ratingText.split(' ')[0]);
const newRatingElement = document.createElement('span');
newRatingElement.textContent = ratingValue + ' ';
newRatingElement.style.fontWeight = 'bold';
newRatingElement.style.color = '#007600'; // Change color if needed
newRatingElement.style.marginRight = '5px'; // Add some spacing between the ratings
ratingElement.parentElement.parentElement.insertBefore(newRatingElement, ratingElement.parentElement);
r###lt.dataset.ratingValue = ratingValue;
r###lt.classList.add('enhanced-rating');
}
};
// Function to add filter dropdown and refresh icon
const addFilterDropdown = () => {
const filterBar = document.createElement('div');
filterBar.id = 'rating-filter-bar';
const filterLabel = document.createElement('span');
filterLabel.textContent = 'Filter by star rating: ';
filterLabel.style.marginRight = '5px';
filterBar.appendChild(filterLabel);
const filterDropdown = document.createElement('select');
filterDropdown.style.marginRight = '10px';
// Define dropdown options and their actions
const filters = [
{ text: 'All', score: 0 },
{ text: '4.9+', score: 4.9 },
{ text: '4.8+', score: 4.8 },
{ text: '4.7+', score: 4.7 },
{ text: '4.6+', score: 4.6 },
{ text: '4.5+', score: 4.5 },
{ text: '4.4+', score: 4.4 },
{ text: '4.3+', score: 4.3 },
{ text: '4.2+', score: 4.2 },
{ text: '4.1+', score: 4.1 }
];
filters.forEach(filter => {
const option = document.createElement('option');
option.value = filter.score;
option.textContent = filter.text;
filterDropdown.appendChild(option);
});
filterDropdown.addEventListener('change', (event) => {
currentFilter = parseFloat(event.target.value);
filterR###lts(currentFilter);
});
filterBar.appendChild(filterDropdown);
const refreshIcon = document.createElement('span');
refreshIcon.innerHTML = '↻'; // HTML entity for refresh icon
refreshIcon.style.cursor = 'pointer';
refreshIcon.style.marginLeft = '5px';
refreshIcon.addEventListener('click', () => {
filterR###lts(currentFilter);
});
filterBar.appendChild(refreshIcon);
// Insert the filter bar at the top of the search r###lts
const searchR###lts = document.querySelector('div.s-main-slot.s-r###lt-list.s-search-r###lts.sg-row');
if (searchR###lts) {
searchR###lts.insertBefore(filterBar, searchR###lts.firstChild);
}
};
// Function to filter r###lts based on rating score
const filterR###lts = (minScore) => {
const r###lts = document.querySelectorAll('div[data-asin]:not(.s-pagination-container)');
r###lts.forEach(r###lt => {
if (!r###lt.querySelector('.s-pagination-container')) {
const ratingValue = parseFloat(r###lt.dataset.ratingValue);
r###lt.style.display = ratingValue >= minScore ? '' : 'none';
}
});
};
// Function to process search r###lts
const processSearchR###lts = () => {
const searchR###lts = document.querySelectorAll('div[data-asin]:not(.s-pagination-container)');
searchR###lts.forEach(r###lt => {
enhanceRatingDisplay(r###lt);
});
filterR###lts(currentFilter);
};
// Throttle function to limit the rate of execution
const throttle = (func, delay) => {
let timeoutId = null;
return (...args) => {
if (timeoutId === null) {
func(...args);
timeoutId = setTimeout(() => {
timeoutId = null;
}, delay);
}
};
};
// Throttled version of processSearchR###lts
const throttledProcessSearchR###lts = throttle(processSearchR###lts, 500);
// Observe changes in the search r###lts
const observeSearchR###lts = () => {
const searchR###ltsContainer = document.querySelector('div.s-main-slot.s-r###lt-list.s-search-r###lts.sg-row');
if (searchR###ltsContainer) {
const observer = new MutationObserver(throttledProcessSearchR###lts);
observer.observe(searchR###ltsContainer, {
childList: true,
subtree: true
});
}
};
// Initialize the script
const init = () => {
addFilterDropdown();
processSearchR###lts();
observeSearchR###lts();
};
// Run the script when the page has loaded
window.addEventListener('load', init);
})();