From e60c01bd7853fec5dd0dea6b85ae73a28d5699ef Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 12 Jul 2012 17:19:50 +0200 Subject: [PATCH] Add a cooldown to post deletion. Close #586 --- 4chan_x.user.js | 49 +++++++++++++++++++++++++++++++++++++++++++++---- changelog | 1 + script.coffee | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 8b6cc557b..880b1c5c0 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -4115,21 +4115,37 @@ return true; } }); - return Menu.addEntry({ + Menu.addEntry({ el: div, open: function(post) { - return !post.isArchived; + var node, seconds; + if (post.isArchived) { + return false; + } + node = div.firstChild; + if (seconds = DeleteLink.cooldown[post.ID]) { + node.textContent = "Delete (" + seconds + ")"; + DeleteLink.cooldown.el = node; + } else { + node.textContent = 'Delete'; + delete DeleteLink.cooldown.el; + } + return true; }, children: children }); + return $.on(d, 'QRPostSuccessful', this.cooldown.start); }, "delete": function() { var board, form, id, m, menu, pwd, self; + menu = $.id('menu'); + id = menu.dataset.id; + if (DeleteLink.cooldown[id]) { + return; + } $.off(this, 'click', DeleteLink["delete"]); this.textContent = 'Deleting...'; pwd = (m = d.cookie.match(/4chan_pass=([^;]+)/)) ? decodeURIComponent(m[1]) : $.id('delPassword').value; - menu = $.id('menu'); - id = menu.dataset.id; board = $('a[title="Highlight this post"]', $.id(menu.dataset.rootid)).pathname.split('/')[1]; self = this; form = { @@ -4166,6 +4182,31 @@ error: function(self) { self.textContent = 'Connection error, please retry.'; return $.on(self, 'click', DeleteLink["delete"]); + }, + cooldown: { + start: function(e) { + return DeleteLink.cooldown.count(e.detail.postID, 30); + }, + count: function(postID, seconds) { + var el; + if (!((0 <= seconds && seconds <= 30))) { + return; + } + setTimeout(DeleteLink.cooldown.count, 1000, postID, seconds - 1); + el = DeleteLink.cooldown.el; + if (seconds === 0) { + if (el != null) { + el.textContent = 'Delete'; + } + delete DeleteLink.cooldown[postID]; + delete DeleteLink.cooldown.el; + return; + } + if (el != null) { + el.textContent = "Delete (" + seconds + ")"; + } + return DeleteLink.cooldown[postID] = seconds; + } } }; diff --git a/changelog b/changelog index 9ee3b9f96..8ddf19b02 100644 --- a/changelog +++ b/changelog @@ -1,6 +1,7 @@ master - Mayhem Divide the Delete Link in the Menu into a Post and Image deletion links. + The Delete Links in the Menu now have a cooldown. 2.34.1 - Mayhem diff --git a/script.coffee b/script.coffee index b3f2932e6..ec8ba1b66 100644 --- a/script.coffee +++ b/script.coffee @@ -3268,10 +3268,26 @@ DeleteLink = Menu.addEntry el: div - open: (post) -> !post.isArchived + open: (post) -> + if post.isArchived + return false + node = div.firstChild + if seconds = DeleteLink.cooldown[post.ID] + node.textContent = "Delete (#{seconds})" + DeleteLink.cooldown.el = node + else + node.textContent = 'Delete' + delete DeleteLink.cooldown.el + true children: children + $.on d, 'QRPostSuccessful', @cooldown.start + delete: -> + menu = $.id 'menu' + {id} = menu.dataset + return if DeleteLink.cooldown[id] + $.off @, 'click', DeleteLink.delete @textContent = 'Deleting...' @@ -3281,8 +3297,6 @@ DeleteLink = else $.id('delPassword').value - menu = $.id 'menu' - id = menu.dataset.id board = $('a[title="Highlight this post"]', $.id menu.dataset.rootid).pathname.split('/')[1] self = @ @@ -3314,6 +3328,21 @@ DeleteLink = self.textContent = 'Connection error, please retry.' $.on self, 'click', DeleteLink.delete + cooldown: + start: (e) -> + DeleteLink.cooldown.count e.detail.postID, 30 + count: (postID, seconds) -> + return unless 0 <= seconds <= 30 + setTimeout DeleteLink.cooldown.count, 1000, postID, seconds-1 + {el} = DeleteLink.cooldown + if seconds is 0 + el?.textContent = 'Delete' + delete DeleteLink.cooldown[postID] + delete DeleteLink.cooldown.el + return + el?.textContent = "Delete (#{seconds})" + DeleteLink.cooldown[postID] = seconds + ReportLink = init: -> a = $.el 'a',