Advertisement
gracefulally

Comments: New XKit Extension

Oct 31st, 2015
39,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //* TITLE Comments **//
  2. //* VERSION 0.0.1 **//
  3. //* DESCRIPTION Leave a comment on a Tumblr post. **//
  4. //* DETAILS This extension allows you to comment on original posts on your dashboard. **//
  5. //* DEVELOPER wolfspirals **//
  6. //* FRAME false **//
  7. //* BETA false **//
  8. //* SLOW true **//
  9.  
  10. XKit.extensions.comments = new Object({
  11.  
  12.     running: false,
  13.         slow: true,
  14.        
  15.     run: function() {
  16.         this.running = true;
  17.                 XKit.tools.init_css("timestamps");
  18.                 try {
  19.             if (this.is_compatible()) {
  20.                 XKit.tools.add_css('#posts .post .post_content { padding-top: 0px; }', "timestamps");
  21.                 XKit.post_listener.add("comments", this.add_commenters);
  22.                 this.add_commenters();
  23.                                
  24.                                 $(document).on("submit",".reply form", XKit.extensions.comments.submit_comment);
  25.             }
  26.         } catch(e) {
  27.             show_error_script("Comments: " + e.message);
  28.         }
  29.     },
  30.  
  31.     is_compatible: function() {
  32.         return !(XKit.interface.where().queue || XKit.interface.where().drafts || XKit.interface.where().search || XKit.interface.where().inbox);
  33.     },
  34.  
  35.         add_commenters: function() {
  36.                 var posts = $(".posts .post.is_original").not(".xkit_comments");
  37.  
  38.         if (posts.length === 0) {
  39.             return;
  40.         }
  41.  
  42.                 posts.each(function() {
  43.             var post = $(this);
  44.             post.addClass("xkit_comments");
  45.  
  46.                         var post_id = post.attr('data-post-id');
  47.                         var key = post.attr('data-tumblelog-key');
  48.                         var commentbox_html = '<div class="post_control reply" title="Reply"><div class="popover popover_gradient popover_post_tools" style="display: none;"><div class="popover_inner"><form action="https://tumblr.com/reply"><textarea name="reply_text" maxlength="250" title="250 max" style="background-image: none; background-position: 0% 0%; background-repeat: repeat;"></textarea><button type="submit" class="chrome blue" data-label-loading="Replying..." data-label="Reply" style="width:100%" disabled="disabled">Reply</button></form></div></div></div>';
  49.  
  50.                         post.find(".post_controls_inner").append(commentbox_html);
  51.             });
  52.         },
  53.  
  54.         submit_comment: function(e) {
  55.                 e.preventDefault();
  56.                 var comment_form = $(this);
  57.  
  58.                 $.ajax({
  59.             type: "POST",
  60.             url: "/reply",
  61.             data: {
  62.                                 reply_text: " " + comment_form.find("textarea").get(0).value,
  63.                                 post_id: comment_form.parents(".post").attr("data-post-id"),
  64.                                 key: comment_form.parents(".post").attr("data-tumblelog-key")
  65.                         },
  66.                         cache: true,
  67.             dataType: "jsonp",
  68.                         crossDomain: true,
  69.                         jsonp: false,
  70.                         jsonpCallback: function() {
  71.                                 setTimeout(XKit.extensions.comments.click_dialog, 10);
  72.                         }
  73.         });
  74.         },
  75.  
  76.        click_dialog: function(){
  77.                if($(".ui_dialog_pos").length > 0) {
  78.                        $(".ui_dialog_pos button").click();
  79.                        $(".ui_dialog_lock").hide();
  80.                        var reply = $(".reply.active");
  81.                        reply.removeClass("active");
  82.                        reply.find(".popover").css("display", "none");
  83.                        reply.find("form textarea").val("");
  84.                        reply.find("form button").text("Reply");
  85.                } else {
  86.                        setTimeout(XKit.extensions.comments.click_dialog, 10);
  87.                }
  88.        },
  89.  
  90.     destroy: function() {
  91.         this.running = false;
  92.                 $(".reply").remove();
  93.         $(".xkit_comments").removeClass("xkit_comments");
  94.         XKit.tools.remove_css("comments");
  95.         XKit.post_listener.remove("comments");
  96.     }
  97.  
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement