// ==UserScript== // @name SG User Notes // @namespace https://www.steamgifts.com // @version 0.6 // @description Save notes about other users on steamgifts.com // @author MH // @downloadURL https://raw.githubusercontent.com/maherm/steamgifts_usernotes/master/sg_usernotes.user.js // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js // @include http*://www.steamgifts.com/user/* // @include http*://www.steamgifts.com/account/* // @grant GM_setValue // @grant GM_getValue // @grant GM_listValues // @grant GM_deleteValue // @grant GM_addStyle // @grant unsafeWindow // ==/UserScript== (function() { 'use strict'; //=====================Constants============================== var NoteTypes = { _removePostfix : "_remove", Comment: "comment", Blacklist: "blacklist", BlacklistRemove : "blacklist_remove", Whitelist: "whitelist", WhitelistRemove: "whitelist_remove" }; var ImportModes = { ReplaceAll : "ReplaceAll", ReplaceIfExists : "ReplaceIfExists", RetainAll : "RetainAll" }; var IconFactory = {}; IconFactory[NoteTypes.Comment] = function(){return $("");}; IconFactory[NoteTypes.Blacklist] = function(){return $("");}; IconFactory[NoteTypes.BlacklistRemove] = function(){return $("");}; IconFactory[NoteTypes.Whitelist] = function(){return $("");}; IconFactory[NoteTypes.WhitelistRemove] = function(){return $("");}; var DisplayText = {}; DisplayText[NoteTypes.Comment] = ""; DisplayText[NoteTypes.Blacklist] = "Added to blacklist"; DisplayText[NoteTypes.BlacklistRemove] = "Removed from blacklist"; DisplayText[NoteTypes.Whitelist] = "Added to whitelist"; DisplayText[NoteTypes.WhitelistRemove] = "Removed from whitelist"; var overridden_url = "https://www.steamgifts.com/account/manage/whitelist"; var settings_url = overridden_url +"#userNotes"; //====================Globals================================= var user_notes; var settings; var notification_queue = []; //====================Main==================================== function main(){ showNotifications(); if(isSelf()) return; settings = readSettings(); if(isAccountPage()){ injectCss(); addUserNotesLink(); if(isUserNotesSettingsPage()){ switchToUserNotesSettings(); } if(isOverriddenPage()){ initUserNotesLink(); } }else{ injectCss(); //GM_deleteValue(getUserId()); //for Debugging loadNotes(); createNotesButton(); createPanel(); initButtons(); } } function showNotifications(){ if(isUsingTampermonkey() && GM_info.script.downloadURL.indexOf("_ff.user.js") >0 ){ var $content = $("There is a bug in your version of SG User Notes which broke the update mechanism. Please re-install the userscript using this link. Your saved user notes will not be affected"); queueNotification("Hotfix1_brokenUpdate", createNotification("danger", $content), {showAlways: true}); } //queueNotification("Release_0.6", createNotification("success", "SG User Notes: New Version: 0.6 - No new features", {noQueue: true})); } function loadNotes(){ var userId = getUserId(); user_notes = loadNotesForUser(userId); } function getAllSavedUserIds(){ return GM_listValues().filter(function(el){return !isNaN(el);}); } function getExportData(){ var userIds = getAllSavedUserIds(); var vals = {}; for (var i=0; i 1) console.error("Multiple notes with same id properties: "+noteType+", "+date); if(toChange.length === 1){ toChange[0].text = text; }else{ current.push({type: noteType, date: date, text: text}); } GM_setValue(userId, JSON.stringify(current)); incSyncRevision(); loadNotes(); updateNotesButton(); renderNotes(); } function readSettings(){ var defaultSettings = { reverseNoteDirection: false }; return GM_getValue("settings", defaultSettings); } function saveSettings(){ GM_setValue("settings", settings); } function loadSync(){ var result = GM_getValue("sync", undefined); if(result === undefined){ result = {}; result.uuid = uuid(); result.rev = 1; saveSync(result); } return result; } function saveSync(sync){ GM_setValue("sync", sync); } function incSyncRevision(){ var sync = loadSync(); sync.rev += 1; saveSync(sync); } function isUsingTampermonkey(){ return GM_info.toString().indexOf("Tampermonkey") >=0; } //========================= Settings Page Functions ======================== /* from http://stackoverflow.com/a/8809472/1842905 */ function uuid(){ var d = new Date().getTime(); if(window.performance && typeof window.performance.now === "function"){ d += performance.now(); //use high-precision timer if available } var result = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x3|0x8)).toString(16); }); return result; } /* from http://stackoverflow.com/a/26298948 */ function readSingleFile(e, successCallback) { var file = e.target.files[0]; if (!file) { return; } var reader = new FileReader(); reader.onload = function(e) { var contents = e.target.result; successCallback(contents); }; reader.readAsText(file); } function getUserKeys(json){ return Object.keys(json).filter(function(el){return !isNaN(el);}); } function importJson(jsonStr, importMode){ var json = JSON.parse(jsonStr); var userKeys = getUserKeys(json); if(importMode === ImportModes.ReplaceAll){ if (!confirm('Import Mode: Replace all\nImporting will delete ALL previously saved user notes. Are you sure?')) { return; } deleteNotes(getAllSavedUserIds()); } if(importMode === ImportModes.ReplaceExistingUsers){ if (!confirm('Import Mode: Replace existing\nImporting will override all entries for a user if there is any note for this user in the imported file. Are you sure?')) { return; } deleteNotes(userKeys); } if(importMode === ImportModes.RetainAll){ if (!confirm('Import Mode: Retain all\nImporting will retain all previously saved data. This may lead to duplicate entries. Are you sure?')) { return; } } var importedNotesCounter = 0; for(var i=0; i").addClass("sidebar__shortcut__sgun_comments").attr("style", "opacity: 1;").attr("data-tooltip",""); var $icon = $(""); $btn.append($icon); $(".sidebar__shortcut-inner-wrap").append($btn); $btn.addClass("sgun__notes_present"); $icon.removeClass("fa-lg").addClass("fa-stack-1x"); var $badge = $("").addClass("fa fa-stack-1x fa-inverse sgun__badge"); var $span = $("").addClass("fa-stack fa-lg").append($icon).append($badge); $btn.append($span); updateNotesButton(); } function updateNotesButton(){ var $icon = $(".sgun__icon"); var $badge = $icon.siblings(".sgun__badge"); if(user_notes.length > 0){ $icon.removeClass("fa-comment-o").addClass("fa-comment"); $badge.text(user_notes.length); }else{ $icon.removeClass("fa-comment").addClass("fa-comment-o"); $badge.text(""); } } function createPanel(){ var $panel = $("
").addClass("sgun_notes_panel").addClass("sidebar__shortcut-tooltip-absolute").hide(); $(".sidebar__shortcut-tooltip-relative").append($panel); } function destroyNewNotePanel(){ $(".sgun_new_note").next().show().end().remove(); $(".sidebar__shortcut-tooltip-relative").show(); } function createNewNotePanel(noteType, date){ //Remove old panel destroyNewNotePanel(); //Create Button var $button = $(""); //Create input field var $input = $("").addClass("sidebar__search-input").attr("placeholder", "Your note here..."); //Create form var $form = $("
").append($input); $form.append($("").attr("name","noteType").val(noteType)); $form.append($("").attr("name","noteDate").val(date)); //Create panel var $panel = $("
").addClass("sgun_new_note").addClass("sidebar__search-container").addClass("sgun_new_note__"+noteType).hide(); $panel.append($form); $panel.append($button); //Add panel to DOM $(".sidebar__search-container").before($panel); //Init Controls //Switch between "Save" and "Clear" button $input.on("input paste", function(){ var btn=$(this).closest(".sgun_new_note").find(".sgun_button"); btn.removeClass("fa-remove fa-save"); btn.addClass($(this).val()==="" ? "fa-remove" : "fa-save"); }); //Submit on "enter" $input.keypress(function(e) { if(e.which == 13) { e.preventDefault(); $button.click(); } }); //Submit action $button.on("click", function(){ if($(this).hasClass("fa-save")) saveData(noteType, date, $input.val()); destroyNewNotePanel(); }); //Show the newly created Panel $(".sgun_new_note").show().next().hide(); $(".sidebar__shortcut-tooltip-relative").hide(); $input.focus(); } function showNotes(){ renderNotes(); $(".sgun_notes_panel").show().siblings().hide(0); } function hideNotes(){ $(".sgun_notes_panel").hide().siblings().show(0); } function renderNotes(){ var $panel = $(".sgun_notes_panel"); $panel.empty(); if(user_notes.length === 0){ $panel.append($("
No notes for this user
")); } var i = 0; var step = 1; var condition= function(i){return i=0;}; step = -1; } for(i; condition(i); i+=step){ var note = user_notes[i]; var $note_html = $("
").addClass("sgun_note").addClass("sgun_note__"+note.type); $note_html.append($("").addClass("sgun_note_type").append(IconFactory[note.type]())); $note_html.append($("").addClass("sgun_note_date").text(moment(note.date).format("YYYY-MM-DD HH:mm"))); $note_html.append($("").addClass("sgun_note_text").text( note.text)); $panel.append($note_html); } } function createNotification(type, $content){ if(!($content instanceof jQuery)) $content = $(""+$content+""); else $content = $("").append($content); var $result = $("
").addClass("sgun__alert").addClass("alert alert-"+type).append($content); return $result; } function queueNotification(name, $html, opt){ var db_name = "notification_"+name; var savedData = GM_getValue(db_name); var options = $.extend({showAlways: false, closable: true, key: db_name, noQueue: false}, opt); if(options.showAlways || savedData === undefined || savedData.dismissed === false){ if($(".sgun__alert").length === 0){ showNotification($html, options); }else if(!options.noQueue){ notification_queue.push({$html: $html, options: options}); } } } function showNextNotification(){ var queued = notification_queue.pop(); if(queued !== undefined){ showNotification(queued.$html, queued.options); } } function showNotification($html, options){ if(options.closable === true){ var $button = $(""); $html.append($button); $button.click(function(){ $html.slideUp().queue(function(){$html.remove();}); markNotificationAsDismissed(options); showNextNotification(); }); } $html.hide(); $(".sidebar").next("div").prepend($html); $html.slideDown(); } function markNotificationAsDismissed(options){ if(!options.showAlways){ var val = GM_getValue(options.key, {}); val.dimissed = true; GM_setValue(options.key,val); } } //=================================UI Elements Settings Page ======================== function addUserNotesLink(){ var $link = $(''); var $li = $('