Skip to content

Instantly share code, notes, and snippets.

@skoskie
Created October 30, 2015 17:50
Show Gist options
  • Save skoskie/46d8fc5a6d4420878569 to your computer and use it in GitHub Desktop.
Save skoskie/46d8fc5a6d4420878569 to your computer and use it in GitHub Desktop.
Highlight Compensated Reviews on Amazon.com
// Amazon pages seem to intermittently have jQuery loaded.
if (!window.jQuery) {
var jqlib = document.createElement('script');
jqlib.setAttribute('type', 'text/javascript');
jqlib.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js');
document.head.appendChild(jqlib);
}
// Make sure jQuery before executing.
function defer(method) {
if (window.jQuery)
method();
else
setTimeout(function() { defer(method) }, 50);
}
// Highlights specific terms found in reviews.
// Add or remove terms from the 'search_terms' array.
// Update review selectors in the 'search_range' jQuery object.
highlightCompensatedReviews = function(){
var d = document;
var $ = jQuery;
// Verify we are on Amazon.
var domain = window.location.host;
var domain_parts = domain.split('.');
if(domain_parts[domain_parts.length - 1] != 'com' || domain_parts[domain_parts.length - 2] != 'amazon'){
console.log("This is not Amazon. Exiting.");
return; //GTFO
}
// Else, carry on.
$(d).ready(function()
{
// Terms to mark
var search_terms = [
'unbiased',
'discount in exchange',
'in exchange for',
'discounted price',
'for my honest review',
'for an honest review'
];
// Places to search for the search_terms
var search_range = $('#revMHRL [class="a-section"]') // Product Page - Main - Most Helpful Customer Reviews
.add('#revMRRL [class="a-row a-spacing-base"]') // Product Page - Sidebar - Most Recent Customer Reviews
.add('#cm_cr-review_list .review-text') // Review Page - Main
.add($('.view-point-review .a-size-base').parent()); // Review Page - Main
// Add more sections to search as they are found.
// For each review.
search_range.each(function(index, el) {
var text = $(el).html();
// For each search term
for (var i = 0; i < search_terms.length; i++) {
text = text.replace(search_terms[i], '<mark class="compensation-term">' + search_terms[i] + '</mark>');
}
$(el).html(text);
});
// Modify styles for marked search terms.
$('.compensation-term').css({
'padding': '1px 2px'
});
// Modify styles for reviews containing marked search terms.
var biased_reviews = search_range.has('.compensation-term');
biased_reviews.css({
'background-color': 'rgba(255, 127, 80, 0.15)',
'opacity': '0.55'
});
});
};
defer(highlightCompensatedReviews);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment