patternjavascriptMinor
Stack Exchange Post Reminder
Viewed 0 times
exchangepostreminderstack
Problem
I've created a UserScript for adding follow-up reminders to any post (question or answer) here on the Stack Exchange network. I did this in response to a stackoverflow meta post feature request which sparked my interest.
It adds a calendar icon into the vote cell which displays a
Reminders are displayed at the top of the screen in the navbar alongside your inbox/achievements and can be dismissed by clicking on them.
Everything works but feels sloppy/spaghetti-ish and I would like to get some feedback on how I can improve it, I'm sure I made a few mistakes.
```
var Reminder = function (reminderId, postId, postUrl, postTitle, postType, siteName, reminderDate) {
this.reminderId = reminderId;
this.postId = postId;
this.postUrl = postUrl;
this.postTitle = postTitle;
this.postType = postType;
this.siteName = siteName;
this.reminderDate = reminderDate;
};
var Reminders = {
Add(reminder) {
reminders[reminder.reminderId] = {
"reminderId": reminder.reminderId,
"postId": reminder.postId,
"postUrl": reminder.postUrl,
"postTitle": reminder.postTitle,
"postType": reminder.postType,
"siteName": reminder.siteName,
"reminderDate": reminder.reminderDate
};
},
Clear() {
reminders = {};
},
HasReminder() {
return reminders.hasOwnProperty(reminderId);
},
Load() {
if (GM_getValue('reminders', undefined) == undefined) {
GM_setValue('reminders', JSON.stringify(reminders));
} else {
reminders = JSON.parse(GM_getValue('reminders'));
}
},
Remove(reminderId) {
delete reminders[reminderId];
},
Save() {
GM_setValue('reminders', JSON.stringify(reminders));
}
It adds a calendar icon into the vote cell which displays a
datepicker where you can select a reminder date at which time you'll be notified via a notification dialog similar to the current inbox and achievements dialog.Reminders are displayed at the top of the screen in the navbar alongside your inbox/achievements and can be dismissed by clicking on them.
Everything works but feels sloppy/spaghetti-ish and I would like to get some feedback on how I can improve it, I'm sure I made a few mistakes.
reminders.js```
var Reminder = function (reminderId, postId, postUrl, postTitle, postType, siteName, reminderDate) {
this.reminderId = reminderId;
this.postId = postId;
this.postUrl = postUrl;
this.postTitle = postTitle;
this.postType = postType;
this.siteName = siteName;
this.reminderDate = reminderDate;
};
var Reminders = {
Add(reminder) {
reminders[reminder.reminderId] = {
"reminderId": reminder.reminderId,
"postId": reminder.postId,
"postUrl": reminder.postUrl,
"postTitle": reminder.postTitle,
"postType": reminder.postType,
"siteName": reminder.siteName,
"reminderDate": reminder.reminderDate
};
},
Clear() {
reminders = {};
},
HasReminder() {
return reminders.hasOwnProperty(reminderId);
},
Load() {
if (GM_getValue('reminders', undefined) == undefined) {
GM_setValue('reminders', JSON.stringify(reminders));
} else {
reminders = JSON.parse(GM_getValue('reminders'));
}
},
Remove(reminderId) {
delete reminders[reminderId];
},
Save() {
GM_setValue('reminders', JSON.stringify(reminders));
}
Solution
reminders.js is good, but you don't need to return an array in for var reminders:Use the
prototype chain instead:var Reminders = function(){};
Reminders.prototype.Add = function(reminder){};
Reminders.prototype.Clear = function(){};
Reminders.prototype.HasReminder = function(){};
Reminders.prototype.Load = function(){};
Reminders.prototype.Remove = function(reminderId){};
Reminders.prototype.Save = function(){};That way you get rid of the extra level of indentation.
Your
post-reminders.user.js file is a little different.I found a lot of instances of massive strings to be appended.
Consider using
document.createElement instead of strings.What I also saw is that the jQuery you have could be replaced with vanilla JavaScript equivalents, meaning you could chop the library entirely out of your code.
Code Snippets
var Reminders = function(){};
Reminders.prototype.Add = function(reminder){};
Reminders.prototype.Clear = function(){};
Reminders.prototype.HasReminder = function(){};
Reminders.prototype.Load = function(){};
Reminders.prototype.Remove = function(reminderId){};
Reminders.prototype.Save = function(){};Context
StackExchange Code Review Q#114586, answer score: 7
Revisions (0)
No revisions yet.