// ==UserScript==
// @name gathering.tweakers.net - Add Topic Bookmark
// @homepageURL https://github.com/sand3r/tampermonkey-scripts/tree/master/gathering_of_tweakers
// @namespace gathering.tweakers.net
// @version 0.052
// @description Makes the bookmark button for a post on the GoT Forum (gathering.tweakers.net) work asynchronously. Click Bookmark and wait for it to turn Green.
// @match *://gathering.tweakers.net/forum/myreact
// @match *://gathering.tweakers.net/forum/list_message*
// @include http://gathering.tweakers.net
// @require http://code.jquery.com/jquery-latest.js
// @author Sander Thalen
// ==/UserScript==
jQuery(document).ready(function() {
var bookmarkManager = (function(_$) {
var bookMarkButtons = _$("a[href^='http://gathering.tweakers.net/forum/insert_bookmark/']");
function getCurrentStatus() {
var myReactBookmarks = jQuery('#mybookmarks .topic a:first-child[title]'),
messageIds = [],
messageId = '';
myReactBookmarks.each(function(i, el) {
messageId = el.href.split('#');
messageIds.push(messageId[1]);
});
localStorage.setItem('sndr_bookmarks', messageIds.join(';'));
}
function bindBookmarkButtons() {
var $button;
var messageId;
var bookmarked = localStorage.getItem('sndr_bookmarks');
bookMarkButtons.each(function(i, object) {
$button = _$(object);
messageId = $button.parents('.message').prev('a').attr('name');
// Tag buttons
$button.attr('data-message-id', messageId);
// Add event handler
$button.on('click', function(ev) {
ev.preventDefault();
var target = _$(ev.currentTarget);
// Get required values
var author = target.parents('.message').find('.username a')[0].innerText,
topicName = _$('h1')[0].innerText,
name = author + ' in ' + '"' + topicName + '"',
folder = '11',
reactId = _$('[name="data[reactid]"]').val(),
action = 'insert_bookmark',
messageId = target.attr('data-message-id'),
topicId = _$('[data-topicid]').attr('data-topicid'),
httpReferrer = window.location.href,
button = target;
// Make call
bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer);
});
// Check if any of the bookmark buttons match the values in local storage
if(bookmarked.indexOf(messageId) !== -1) {
markButton($button);
}
});
}
function markButton(button) {
// Change color on button
button.prepend('<img src="http://tweakimg.net/g/icons/checkmark-green.png" class="js_tm_bookmarked_state" /> ');
if(!button.hasClass('js_tm_bookmark_button')) {
button.addClass('js_tm_bookmark_button');
}
button.css({
'color': 'rgb(119, 164, 0)',
'font-weight': 'bold'
});
}
function unmarkButton() {
_$('.js_tm_bookmarked_state').remove();
_$('.js_tm_bookmark_button').removeAttr('style').empty().text('Bookmark');
}
function bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer) {
_$.ajax({
url: 'http://gathering.tweakers.net/forum',
type: 'POST',
data: {
'data[name]' : name,
'data[folder]' : folder,
'data[reactid]' : reactId,
'action' : action,
'data[messageid]' : messageId,
'data[topicid]' : topicId,
'data[http_referrer]' : httpReferrer
}
})
.success(function() {
unmarkButton();
markButton(button);
});
}
return {
bindBookmarkButtons: bindBookmarkButtons,
getCurrentStatus: getCurrentStatus
};
})(jQuery);
if(window.location.pathname === '/forum/myreact') {
bookmarkManager.getCurrentStatus();
} else {
bookmarkManager.bindBookmarkButtons();
}
});