NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name HueTemplateEvaluator // @namespace http://search.coupang.com // @version 0.1 // @description Evaluate the text field in Hue as a template // @author Uhyon Chung (Luther) // @match http://*/notebook/editor* // @grant none // @require https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.js // ==/UserScript== var evalTextAreaIsOpen = false; function onEvaluate() { var re = new RegExp("\\$\\{.*}}", 'g'); var query = $("#evalTextArea").val(); var contextStr = $("#evalContextArea").val(); query = query.replace(/\$\{([^}]*)\}/g, "{{$1}}"); var context = JSON.parse("{" + contextStr + "}"); var template = Handlebars.compile(query); var result = template(context); window.replaceAce(result); } function onEvaluatorClick() { $(".btn-group").parent().toggle(); $(".query-name,.notebook-name-desc,#evaluate,#evalContextArea,#evalTextArea").toggle(); if (evalTextAreaIsOpen) { $("#evalTextArea").hide(); $("#evalContextArea").hide(); $("#evaluate").hide(); evalTextAreaIsOpen = false; } else { if ($("#evalTextArea").length > 0) { $("#evalTextArea").show(); $("#evalContextArea").show(); $("#evaluate").show(); } else { } evalTextAreaIsOpen = true; } } function addDays(date, days) { var result = new Date(date); result.setDate(result.getDate() + days); return result; } function parseDate(str) { str = typeof str === "string" ? str : str.toString(); var y = str.substr(0,4), m = str.substr(4,2) - 1, d = str.substr(6,2); var D = new Date(y,m,d); return (D.getFullYear() == y && D.getMonth() == m && D.getDate() == d) ? D : 'invalid date'; } function formatDate(d) { var mm = d.getMonth() + 1; // getMonth() is zero-based var dd = d.getDate(); return [d.getFullYear(), (mm>9 ? '' : '0') + mm, (dd>9 ? '' : '0') + dd ].join(''); } (function() { 'use strict'; setTimeout(function () { $(".query-name").before("<li><a href='#' style='cursor:pointer;' id='evaluator'>Evaluator</a></li>"); $("#evaluator").click(onEvaluatorClick); $("#evaluator").after("<li><a href='javascript:void(0)' id='evaluate'>Go</a></li>"); $("#evaluate").after("<textarea id='evalContextArea' style='position:relative;width:200px;' rows='10' cols='50' placeholder='Context' style='font-family: monospace;'></textarea>"); $("#evalContextArea").after("<textarea id='evalTextArea' style='position:relative;width:700px;' rows='10' cols='150' placeholder='Template' style='font-family: monospace;'></textarea>"); $("#evaluate").click(onEvaluate); $("#evaluate,#evalContextArea,#evalTextArea").toggle(); }, 2000); Handlebars.registerHelper('daterange', function(startDate, n, block) { var accum = []; var curDate = parseDate(startDate); if (n > 0) { for (var i = 0; i < n; ++i) { accum.push(block.fn({day:formatDate(curDate)})); curDate = addDays(curDate, 1); } } else if (n < 0) { for (var k = 0; k < -n; ++k) { accum.push(block.fn({day:formatDate(curDate)})); curDate = addDays(curDate, -1); } } return accum.join(""); }); Handlebars.registerHelper('hours', function(from, to, block) { var context = {}, mergeContext = function(obj) { for(var k in obj)context[k]=obj[k]; }; mergeContext(this); var accum = []; for(var i = from; i < to; i += 1) { context.hour = ((i < 10 ? '0' : '') + i); accum.push(block.fn(context)); } return accum.join(""); }); })();