Created
January 27, 2017 17:23
-
-
Save rejuvyesh/4069ab38da1e9805f4fa44528dde1ca0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GoodReads Review Management | |
// @namespace [email protected] | |
// @description Show/Hide reviews. State stored between site loads | |
// @include */www.goodreads.com/* | |
// @version 1 | |
// @grant none | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js | |
// ==/UserScript== | |
var STORAGE_ID = 'greasemonkey goodreads hide state'; | |
function getItemState(dom_id) { | |
var item = localStorage.getItem(STORAGE_ID); | |
var state = {}; | |
if (item && item != "") state = JSON.parse(item); | |
if (state.hasOwnProperty(dom_id) && state[dom_id] == 'hide') return 'hide'; | |
else return 'show'; | |
} | |
function setItemState(dom_id, value) { | |
var item = localStorage.getItem(STORAGE_ID); | |
var state = {}; | |
if (item && item != "") state = JSON.parse(item); | |
if (value == 'show') delete state[dom_id]; | |
else state[dom_id] = value; | |
localStorage.setItem(STORAGE_ID, JSON.stringify(state)); | |
} | |
function showHideReview(dom_id, link) { | |
if (getItemState(dom_id) == 'show') hideReview(dom_id, link); | |
else showReview(dom_id, link); | |
} | |
function hideReview(dom_id, link) { | |
$('#'+dom_id).find('.reviewText').hide(); | |
$('#'+dom_id).fadeTo(0, 0.4); | |
setItemState(dom_id, 'hide'); | |
link.text('[+]'); | |
} | |
function showReview(dom_id, link) { | |
$('#'+dom_id).find('.reviewText').show(); | |
$('#'+dom_id).fadeTo(0, 1); | |
setItemState(dom_id, 'show'); | |
link.text('[-]'); | |
} | |
function makeShowHideButtonFor(dom_id) { | |
var link = null; | |
link = $('<a/>', { | |
class: 'right', | |
text: '[-]', | |
style: 'cursor: pointer;' | |
}).on('click', function() { showHideReview(dom_id, link); }); | |
if (getItemState(dom_id) == 'hide') hideReview(dom_id, link); | |
return link; | |
} | |
$('.review').each(function() { | |
var dom_id = $(this).attr('id'); | |
$(this).find('.reviewDate').before(makeShowHideButtonFor(dom_id)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment