Skip to content

Instantly share code, notes, and snippets.

@bhughes339
Last active December 16, 2023 18:45
Show Gist options
  • Save bhughes339/7a9e649e352b1ade6633e48adf068442 to your computer and use it in GitHub Desktop.
Save bhughes339/7a9e649e352b1ade6633e48adf068442 to your computer and use it in GitHub Desktop.
user.js
// ==UserScript==
// @name ReviewMeta Amazon Search
// @namespace http://tampermonkey.net/
// @version 0.1.6
// @description Modify star ratings on Amazon pages based on ReviewMeta scores
// @author bhughes339
// @include https://*.amazon.tld/*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
/* Color legend:
Green - Pass (good)
Yellow - Warn (iffy)
Red - Fail (bad)
Grey - Not yet analyzed by ReviewMeta */
(function() {
'use strict';
function getClassFromScore(score) {
score = parseFloat(score);
var rounded = (Math.round(score * 2) / 2).toString();
return `a-star-${rounded.replace('.', '-')}`;
}
function modStars(asin, ele) {
$.getJSON('https://reviewmeta.com/api/amazon/' + asin, function(data) {
switch (data.s_overall) {
case "1":
ele.attr('style', 'filter:hue-rotate(40deg)');
break;
case "2":
ele.attr('style', 'filter:hue-rotate(380deg)');
break;
case "3":
ele.attr('style', 'filter:hue-rotate(315deg) saturate(500%)');
break;
default:
ele.attr('style', 'filter:grayscale(100%)');
ele.attr('onclick', `window.open('${data.href}');`);
break;
}
// ele.append(` (${data.rating})`);
if (data.rating) {
var classes = `a-icon a-icon-star ${getClassFromScore(data.rating)}`;
ele.find('i').removeClass(function(index, className) {
return (className.match(/a-star(?:[\S]*)?-[\d-]+/) || []).join(' ');
}).addClass(getClassFromScore(data.rating));
var ratingSpan = document.querySelector("#averageCustomerReviews span.a-size-base");
if (ratingSpan) {
if (ratingSpan.innerText.length < 5) {
ratingSpan.innerText += `(${data.rating}) `;
}
}
}
});
}
// Get the ASIN from the data-a-popover attribute of a span item
function getAsinFromSpan(ele) {
var popover = ele.attr('data-a-popover');
if (typeof popover === 'string') {
var asin = popover.match(/asin=([A-Za-z\d]+)/);
if (asin) {
return asin[1];
}
}
return null;
}
var url = window.location.href;
var asinMatch = url.match(/\/(B[\dA-Z]{9}|\d{9}(?:X|\d))/); // https://stackoverflow.com/questions/2123131/determine-if-10-digit-string-is-valid-amazon-asin
var srch = (asinMatch) ? 'span#acrPopover' : 'span.a-declarative';
$(srch).each(function(index) {
var $self = $(this);
var asin = (asinMatch) ? asinMatch[1] : getAsinFromSpan($self);
if (asin) {
modStars(asin, $self);
}
});
})();
@wsoyka
Copy link

wsoyka commented Oct 20, 2017

Would you mind replacing the @match .com with @include .tld? That way all Versions of Amazon (.com, .de, .fr, .es, ....) could use your Script!

You'd just have to change
// @match https://*.amazon.com/s/*
to
// @include https://*.amazon.tld/s/*

Regards and thanks for this nice script!

@bhughes339
Copy link
Author

@wsoyka Thanks for the suggestion! I made the change to the script.

@Lacotis08
Copy link

hola

@virtuallyvlad
Copy link

virtuallyvlad commented May 16, 2021

Any chance you could change the orange on the "warn" grade to yellow or something else?

Amazon now colors its star ratings orange, so for items with warn grades you can't tell if you're looking at the original Amazon rating or the modified ReviewMeta rating. The other colors don't quite match ReviewMeta's either (the green is really pale and the red comes out pink) but at least those are different from Amazon's.

If it helps, ReviewMeta uses these:
Pass - #009e0f (green)
Warn - #f8f850 (yellow)
Fail - #cf2a27 (red)

EDIT: Just noticed you updated this with v0.1.5. Looks good -- thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment