Skip to content

Instantly share code, notes, and snippets.

@adaliabooks
Last active September 9, 2015 20:24
Show Gist options
  • Save adaliabooks/3dd8624a2dd72a7209fe to your computer and use it in GitHub Desktop.
Save adaliabooks/3dd8624a2dd72a7209fe to your computer and use it in GitHub Desktop.
Adds the ability to sort reviews on GoG by a number of criteria
// ==UserScript==
// @name GoG Reviews Filter
// @namespace https://gist.github.com/adaliabooks/
// @version 1.2
// @description Adds the ability to sort reviews on GoG by a number of criteria
// @author adaliabooks
// @updateURL https://gist.github.com/adaliabooks/3dd8624a2dd72a7209fe/raw/
// @downloadURL https://gist.github.com/adaliabooks/3dd8624a2dd72a7209fe/raw/
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
// @grant none
// @include http://www.gog.com/game/*
// @include https://www.gog.com/game/*
// ==/UserScript==
setTimeout(function(){
var versionNumber = 1.2;
var reviewsAngularScope = angular.element(document.getElementById('reviews')).scope();
var reviewFactory = angular.element(document.getElementById('reviews')).injector().get('reviewFactory');
var reviewsURL = window.location.protocol + '//www.gog.com/reviews/product/' + gogData.gameProductData.id + '.json';
var reviewsTotalPages = gogData.gameProductData.reviews.totalPages;
var currentPage = 1;
gogData.reviews.pages = [];
//gogData.gameProductData.reviews.pages = [];
$(".reviews__header").append('<div style="float: right">Reviews Per Page <select id="reviewsPerPage" style="margin-right: 10px"><option value="5" selected="">5</option><option value="10">10</option><option value="15">15</option><option value="20">20</option><option value="50">50</option><option value="0">All</option></select>Sort Order <select id="sortOrder" style=""><option value="date" selected="">Date</option><option value="helpfulness">Helpfulness</option><option value="stars">Stars</option><option value="length">Length</option></select><div><span style="float: right">Version: ' + versionNumber + '</span></div></div><br>Total Reviews: ' + reviewsAngularScope.totalResults);
var allReviewsArray = [];
function sortByDateInner (a, b)
{
return (a.added < b.added) ? 1 : (a.added > b.added) ? -1 : 0;
}
function sortByHelpfulnessInner (a, b)
{
var aHelpfulness = 0;
var bHelpfulness = 0;
if (a.totalVotes != 0)
{
aHelpfulness = (a.helpfulVotes * a.helpfulVotes) / a.totalVotes;
}
if (b.totalVotes != 0)
{
bHelpfulness = (b.helpfulVotes * b.helpfulVotes) / b.totalVotes;
}
return (aHelpfulness < bHelpfulness) ? 1 : (aHelpfulness > bHelpfulness) ? -1 : 0;
}
function sortByStarsInner (a, b)
{
return (a.rating < b.rating) ? 1 : (a.rating > b.rating) ? -1 : 0;
}
function sortByLengthInner (a, b)
{
return (a.description.length < b.description.length) ? 1 : (a.description.length > b.description.length) ? -1 : 0;
}
function sortByDate (a, b)
{
var result = sortByDateInner(a, b);
if (result == 0)
{
result = sortByHelpfulnessInner(a, b);
if (result == 0)
{
result = sortByStarsInner(a, b);
}
}
return result;
}
function sortByHelpfulness (a, b)
{
var result = sortByHelpfulnessInner(a, b);
if (result == 0)
{
result = sortByDateInner(a, b);
if (result == 0)
{
result = sortByStarsInner(a, b);
}
}
return result;
}
function sortByStars (a, b)
{
var result = sortByStarsInner(a, b);
if (result == 0)
{
result = sortByHelpfulnessInner(a, b);
if (result == 0)
{
result = sortByDateInner(a, b);
}
}
return result;
}
function sortByLength (a, b)
{
var result = sortByLengthInner(a, b);
if (result == 0)
{
result = sortByHelpfulnessInner(a, b);
if (result == 0)
{
result = sortByDateInner(a, b);
}
}
return result;
}
function sortReviewsArray()
{
var sortOrder = $("select#sortOrder option:selected").val();
switch (sortOrder) {
case "date":
allReviewsArray.sort(sortByDate);
break;
case "helpfulness":
allReviewsArray.sort(sortByHelpfulness);
break;
case "stars":
allReviewsArray.sort(sortByStars);
break;
case "length":
allReviewsArray.sort(sortByLength);
break;
default:
allReviewsArray.sort(sortByDate);
break;
}
}
function updatePage()
{
var reviewsPerPage = $("select#reviewsPerPage option:selected").val();
if (reviewsPerPage != 0)
{
reviewsAngularScope.reviews = allReviewsArray.slice((reviewsAngularScope.currentPage - 1) * reviewsPerPage, (reviewsAngularScope.currentPage * reviewsPerPage));
reviewsAngularScope.totalPages = Math.ceil(parseInt(reviewsAngularScope.totalResults) / reviewsPerPage);
gogData.reviews.totalPages = reviewsAngularScope.totalPages;
gogData.gameProductData.reviews = reviewsAngularScope.totalPages;
}
else
{
reviewsAngularScope.reviews = allReviewsArray;
reviewsAngularScope.totalPages = 1;
gogData.reviews.totalPages = reviewsAngularScope.totalPages;
gogData.gameProductData.reviews = reviewsAngularScope.totalPages;
}
//reviewsAngularScope.$apply();
reviewsAngularScope.$evalAsync( function (reviewsAngularScope) {
//console.log("$evalAsync");
});
}
function changeReviewsPerPage()
{
reviewsAngularScope.currentPage = 1;
updatePage();
}
function updateReviews()
{
sortReviewsArray();
reviewsAngularScope.currentPage = 1;
updatePage();
}
$('select#reviewsPerPage').on('change', changeReviewsPerPage);
$('select#sortOrder').on('change', updateReviews);
reviewsAngularScope.$watch(
function() { return reviewsAngularScope.currentPage; },
function(newValue, oldValue) {
updatePage();
}
);
reviewsAngularScope.$watch(
function() { return reviewsAngularScope.reviews; },
function(newValue, oldValue) {
if (newValue[0] != oldValue[0])
{
//console.log("Reviews Changed");
updatePage();
}
}
);
reviewsAngularScope.$watch(
function() { return reviewsAngularScope.totalPages; },
function(newValue, oldValue) {
var reviewsPerPage = $("select#reviewsPerPage option:selected").val();
if (reviewsAngularScope.reviews.length != reviewsPerPage)
{
//console.log("Total Pages updated");
updatePage();
}
}
);
$(document).off('ajaxStop');
$( document ).ajaxStop(function() {
updateReviews();
});
for (var i = 1; i <= reviewsTotalPages; i++)
{
var reviewPageURL = reviewsURL + '?page=' + i;
$.getJSON(reviewPageURL, function( data ) {
for (var i = 0; i < data.reviews.length; i++)
{
allReviewsArray.push(reviewFactory.create(data.reviews[i]));
}
//allReviewsArray.push.apply(allReviewsArray, data.reviews)
});
}
},1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment