GoT: Add Topic Bookmark

Makes the bookmark button for a post work asynchronously. Click Bookmark and wait for it to turn Green.

Verze ze dne 29. 08. 2014. Zobrazit nejnovější verzi.

// ==UserScript==
// @name          GoT: Add Topic Bookmark
// @namespace     gathering.tweakers.net
// @version       0.02
// @description   Makes the bookmark button for a post work asynchronously. Click Bookmark and wait for it to turn Green.
// @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() {

  (function(_$) {

    var bookMarkButtons = _$("a[href^='http://gathering.tweakers.net/forum/insert_bookmark/']");

    bookMarkButtons.each(function(i, object) {
      var buttons = _$(object);

      // Add event handler
      buttons.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.parents('.message').prev('a').attr('name'),
            topicId       = _$('[data-topicid]').attr('data-topicid'),
            httpReferrer  = window.location.href,
            button		  = target;
        
        // Make call
        bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer);
        
      });

    });

    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() {
        
        button.css({
          'color':				'rgb(40, 160, 40)',
          'font-weight': 		'bold'
        });
        
      });
  
    }

  })(jQuery);
});