From 0164d311ce4b415d503e88eb1d9cf8205d21c646 Mon Sep 17 00:00:00 2001
From: CertainPerformance
<37674520+CertainPerformance@users.noreply.github.com>
Date: Fri, 1 May 2020 09:02:42 -0500
Subject: [PATCH 1/2] Use a single property in Local Storage instead of many
Fixes #175
---
src/.modules/autoupdater.js | 12 +-
src/.templates/option.html | 8 +-
src/autoreviewcomments.user.js | 362 +++++++++++++++++----------------
3 files changed, 203 insertions(+), 179 deletions(-)
diff --git a/src/.modules/autoupdater.js b/src/.modules/autoupdater.js
index 85f58fc..f25e34f 100644
--- a/src/.modules/autoupdater.js
+++ b/src/.modules/autoupdater.js
@@ -53,23 +53,25 @@ function updateCheck(notifier) {
// - called at the end of the main script if function exists
function CheckForNewVersion(popup) {
var today = (new Date().setHours(0, 0, 0, 0));
- var LastUpdateCheckDay = GetStorage("LastUpdateCheckDay");
- if (LastUpdateCheckDay == null) { //first time visitor
+ var LastUpdateCheckDay = settings.lastUpdateCheckDay;
+ if (LastUpdateCheckDay === 0) { //first time visitor
ShowMessage(popup, "Please read this!", 'Thanks for installing this script. \
Please note that you can EDIT the texts inline by double-clicking them. \
For other options, please see the README at here.',
function() {});
} else if (LastUpdateCheckDay != today) {
updateCheck(function(newver, oldver, install_url) {
- if (newver != GetStorage("LastVersionAcknowledged")) {
+ if (newver !== settings.lastVersionAcknowledged) {
ShowMessage(popup, "New Version!", 'A new version (' + newver + ') of the AutoReviewComments userscript is now available, see the release notes for details or click here to install now.',
function() {
- SetStorage("LastVersionAcknowledged", newver);
+ settings.lastVersionAcknowledged = newver;
+ saveSettings();
});
}
});
}
- SetStorage("LastUpdateCheckDay", today);
+ settings.lastUpdateCheckDay = today;
+ saveSettings();
}
/* How does this work?
diff --git a/src/.templates/option.html b/src/.templates/option.html
index b6fd52f..e24e499 100644
--- a/src/.templates/option.html
+++ b/src/.templates/option.html
@@ -1,8 +1,8 @@
");
var commands = $("
save").click(function() {
- SaveEditable($(this).parent().parent()); UnborkFor(el);
+ SaveEditable($(this).parent().parent());
})
.add("
| ")
.add($("
cancel").click(function() {
- el.siblings(".quick-insert").show(); $(".popup-submit", popup).prop("disabled", false); CancelEditable($(this).parent().parent(), backup); UnborkFor(el);
+ el.siblings(".quick-insert").show(); $(".popup-submit", popup).prop("disabled", false); CancelEditable($(this).parent().parent(), backup);
}));
actions.append(commands);
@@ -393,62 +435,45 @@ with_jquery(function($) {
el.html(txt.add(actions));
}
- //This is to stop the input pinching focus when I click inside textarea
- //Could have done something clever with contentEditable, but this is evil, and it annoys Yi :P
- function BorkFor(el) {
- var label = el.parent("label");
- label.attr("for", "borken");
- }
- function UnborkFor(el) {
- var label = el.parent("label");
- label.attr("for", label.prev().attr("id"));
- }
//Save textarea contents, replace element html with new edited content
- function SaveEditable(el) {
- var html = markDownToHtml(el.find("textarea").val());
- SetStorage(el.attr("id"), Tag(html));
- el.html((showGreeting ? greeting : "") + UnTag(html));
+ function SaveEditable(spanContainingTextarea) {
+ var index = spanContainingTextarea.closest("li").index();
+ var html = markDownToHtml(spanContainingTextarea.find("textarea").val());
+ var commentProp = spanContainingTextarea.hasClass("action-name") ? "name" : "description";
+ settings.comments[index][commentProp] = Tag(html);
+ saveSettings();
+ spanContainingTextarea.html((showGreeting ? greeting : "") + UnTag(html));
}
function CancelEditable(el, backup) {
el.html(backup);
}
- //Empty all custom comments from storage and rewrite to ui
+ // Empty all custom comments from storage and replace with defaults
function ResetComments() {
- ClearStorage("name-"); ClearStorage("desc-");
- $.each(defaultcomments, function(index, value) {
- var targetsPrefix = "";
- if (value.Target) {
- var targets = value.Target.join(",");
- targetsPrefix = "[" + targets + "] ";
- }
- SetStorage("name-" + index, targetsPrefix + value["Name"]);
- SetStorage("desc-" + index, value["Description"]);
- });
- SetStorage("commentcount", defaultcomments.length);
+ settings.comments = getDefaultComments();
+ saveSettings();
}
//rewrite all comments to ui (typically after import or reset)
function WriteComments(popup) {
- if (!GetStorage("commentcount")) ResetComments();
var ul = popup.find(".action-list");
ul.empty();
- for (var i = 0; i < GetStorage("commentcount"); i++) {
- var commentName = GetStorage("name-" + i);
- if (IsCommentValidForPostType(commentName, popup.posttype)) {
- commentName = commentName.replace(Target.MATCH_ALL, "");
- var desc = GetStorage("desc-" + i).replace(/\$SITENAME\$/g, sitename).replace(/\$SITEURL\$/g, siteurl).replace(/\$MYUSERID\$/g, myuserid).replace(/\$/g, "$$$");
- var opt = optionTemplate.replace(/\$ID\$/g, i)
- .replace("$NAME$", commentName.replace(/\$/g, "$$$"))
- .replace("$DESCRIPTION$", (showGreeting ? greeting : "") + desc);
-
- // Create the selectable option with the HTML preview text.
- var optionElement = $(opt);
- $(".action-desc", optionElement).html(desc);
- ul.append(optionElement);
- }
- }
+ var commentsForThisPost = settings.comments.filter(function(comment) {
+ return IsCommentValidForPostType(comment.name, popup.posttype);
+ });
+ commentsForThisPost.forEach(function(comment) {
+ var commentNameToDisplay = comment.name.replace(Target.MATCH_ALL, "").replace(/\$/g, "$$$");;
+ var descriptionToDisplay = UnTag(comment.description).replace(/\$/g, "$$$");
+ var opt = optionTemplate
+ .replace("$NAME$", commentNameToDisplay)
+ .replace("$DESCRIPTION$", (showGreeting ? greeting : "") + descriptionToDisplay);
+
+ // Create the selectable option with the HTML preview text.
+ var optionElement = $(opt);
+ $(".action-desc", optionElement).html(descriptionToDisplay);
+ ul.append(optionElement);
+ });
ShowHideDescriptions(popup);
AddOptionEventHandlers(popup);
AddSearchEventHandlers(popup);
@@ -486,7 +511,7 @@ with_jquery(function($) {
popup.find(".popup-submit").removeAttr("disabled"); //enable submit button
//unset/set selected class, hide others if necessary
$(this).parents("ul").find(".action-selected").removeClass("action-selected");
- if (GetStorage("hide-desc") == "hide") {
+ if (!settings.showDescription) {
$(this).parents("ul").find(".action-desc").hide();
}
$(this).parent().addClass("action-selected")
@@ -532,20 +557,16 @@ with_jquery(function($) {
function AddSearchEventHandlers(popup) {
var sbox = popup.find(".searchbox"),
stext = sbox.find(".searchfilter"),
- kicker = popup.find(".popup-actions-filter"),
- storageKey = "showFilter",
- shown = GetStorage(storageKey) == "show";
+ kicker = popup.find(".popup-actions-filter");
var showHideFilter = function() {
- if (shown) {
+ if (settings.showFilterInput) {
sbox.show();
stext.focus();
- SetStorage(storageKey, "show");
} else {
sbox.hide();
stext.text("");
filterOn(popup, "");
- SetStorage(storageKey, "hide");
}
};
@@ -557,7 +578,8 @@ with_jquery(function($) {
showHideFilter();
kicker.click(function() {
- shown = !shown;
+ settings.showFilterInput = !settings.showFilterInput;
+ saveSettings();
showHideFilter();
return false;
@@ -573,12 +595,12 @@ with_jquery(function($) {
//Adjust the descriptions so they show or hide based on the user's preference.
function ShowHideDescriptions(popup) {
//get list of all descriptions except the currently selected one
- var descriptions = popup.find("ul.action-list li:not(.action-selected) span[id*='desc-']");
+ var descriptions = popup.find("ul.action-list li:not(.action-selected) span.action-desc");
- if (GetStorage("hide-desc") == "hide") {
- descriptions.hide();
- } else {
+ if (settings.showDescription) {
descriptions.show();
+ } else {
+ descriptions.hide();
}
}
@@ -612,12 +634,10 @@ with_jquery(function($) {
//reverse compatible!
function LoadFromRemote(url, done, error) {
GetRemote(url, function(data) {
- SetStorage("commentcount", data.length);
- ClearStorage("name-"); ClearStorage("desc-");
- $.each(data, function(index, value) {
- SetStorage("name-" + index, value.name);
- SetStorage("desc-" + index, markDownToHtml(value.description));
+ settings.comments = data.map(function(comment) {
+ return { name: comment.name, description: markDownToHtml(comment.description) };
});
+ saveSettings();
done();
}, error);
}
@@ -631,8 +651,8 @@ with_jquery(function($) {
var throbber = remote.find("#throbber1");
popup.find(".popup-actions-remote").click(function() {
- urlfield.val(GetStorage("RemoteUrl"));
- autofield.prop("checked", GetStorage("AutoRemote") == "true");
+ urlfield.val(settings.remoteURL);
+ autofield.prop("checked", settings.autoRemote);
remote.show();
});
@@ -643,8 +663,9 @@ with_jquery(function($) {
});
popup.find(".remote-save").click(function() {
- SetStorage("RemoteUrl", urlfield.val());
- SetStorage("AutoRemote", autofield.prop("checked"));
+ settings.remoteURL = urlfield.val();
+ settings.autoRemote = autofield.prop("checked");
+ saveSettings();
remote.hide();
});
@@ -680,7 +701,8 @@ with_jquery(function($) {
popup.find(".welcome-save").click(function() {
var msg = custom.val() == "" ? "NONE" : custom.val();
- SetStorage("WelcomeMessage", msg);
+ settings.welcomeMessage = msg;
+ saveSettings();
greeting = custom.val();
welcome.hide();
});
@@ -907,8 +929,8 @@ with_jquery(function($) {
ImportExport(popup);
});
popup.find(".popup-actions-toggledesc").click(function() {
- var hideDesc = GetStorage("hide-desc") || "show";
- SetStorage("hide-desc", hideDesc == "show" ? "hide" : "show");
+ settings.showDescription = !settings.showDescription;
+ saveSettings();
ShowHideDescriptions(popup);
});
//Handle remote url & welcome
@@ -926,11 +948,11 @@ with_jquery(function($) {
});
//Auto-load from remote if required
- if (!window.VersionChecked && GetStorage("AutoRemote") == "true") {
+ if (!window.VersionChecked && settings.autoRemote) {
var throbber = popup.find("#throbber2");
var remoteerror = popup.find("#remoteerror2");
throbber.show();
- LoadFromRemote(GetStorage("RemoteUrl"),
+ LoadFromRemote(settings.remoteURL,
function() {
WriteComments(popup); throbber.hide();
},
From 4a649e5c3680258ab96f0abac14b20402cc0516e Mon Sep 17 00:00:00 2001
From: CertainPerformance
<37674520+CertainPerformance@users.noreply.github.com>
Date: Fri, 22 May 2020 21:46:13 -0500
Subject: [PATCH 2/2] Save stored comment index with each
Previous version sometimes did not identify the index in the comment array correctly
---
src/autoreviewcomments.user.js | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/src/autoreviewcomments.user.js b/src/autoreviewcomments.user.js
index d4604de..bf3c974 100644
--- a/src/autoreviewcomments.user.js
+++ b/src/autoreviewcomments.user.js
@@ -437,7 +437,7 @@ with_jquery(function($) {
//Save textarea contents, replace element html with new edited content
function SaveEditable(spanContainingTextarea) {
- var index = spanContainingTextarea.closest("li").index();
+ var index = spanContainingTextarea.closest("li").data("index");
var html = markDownToHtml(spanContainingTextarea.find("textarea").val());
var commentProp = spanContainingTextarea.hasClass("action-name") ? "name" : "description";
settings.comments[index][commentProp] = Tag(html);
@@ -459,10 +459,10 @@ with_jquery(function($) {
function WriteComments(popup) {
var ul = popup.find(".action-list");
ul.empty();
- var commentsForThisPost = settings.comments.filter(function(comment) {
- return IsCommentValidForPostType(comment.name, popup.posttype);
- });
- commentsForThisPost.forEach(function(comment) {
+ settings.comments.forEach(function(comment, index) {
+ if (!IsCommentValidForPostType(comment.name, popup.posttype)) {
+ return;
+ }
var commentNameToDisplay = comment.name.replace(Target.MATCH_ALL, "").replace(/\$/g, "$$$");;
var descriptionToDisplay = UnTag(comment.description).replace(/\$/g, "$$$");
var opt = optionTemplate
@@ -470,7 +470,8 @@ with_jquery(function($) {
.replace("$DESCRIPTION$", (showGreeting ? greeting : "") + descriptionToDisplay);
// Create the selectable option with the HTML preview text.
- var optionElement = $(opt);
+ var optionElement = $(opt)
+ .data("index", index);
$(".action-desc", optionElement).html(descriptionToDisplay);
ul.append(optionElement);
});