'use strict'; function JargonBuster(args) { var that = this; this.args = args; this.getDictionary(function(dict) { var tags = that.findTextBlocks(); that.findJargon(tags, dict); that.setupClicks(); }); } function escapeRegExp(str) { //http://stackoverflow.com/a/6969486 //"If you're going to use the function above at least link to this stack //overflow post in your code's documentation so that it doesn't look like //crazy hard-to-test voodoo." return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } JargonBuster.prototype.getDictionary = function(callback) { if ('undefined' !== typeof this.args.spreadsheet_key) { //Okay, we have a spreadsheet url! Do Tabletop. Tabletop.init({ key: this.args.spreadsheet_key, callback: callback, simpleSheet: true }); } else { //Fall back, get dictionary.json var url = chrome.extension.getURL('/dictionary.json'); $.getJSON(url, callback); } } JargonBuster.prototype.findTextBlocks = function() { return $('p'); } JargonBuster.prototype.findJargon = function($tags, dict) { var usedTerms = []; for (var j = 0; j < dict.length; j++) { var terms = dict[j].term.split(','), def = dict[j].definition || dict[j].defintion; $.each(terms, function(i,term){ var tagLeft = '', tagRight = '', term = escapeRegExp(term); $tags.html(function() { var html = $(this).html(), search = new RegExp('(?!]*>)([\.,-\/#!$%\^&\*;:{}=\-_`~() ])(' + term + ')([\.,-\/#!$%\^&\*;:{}=\-_`~() ])(?![^<]*)', 'gi'); if (usedTerms[j] !== true) { html = html.replace(search, "$1" + tagLeft + "$2" + tagRight + "$3"); } if (html.indexOf(term) > -1) { usedTerms[j] = true; } return html; }); }); } } JargonBuster.prototype.setupClicks = function(){ $('.jargon-buster-term').click(function(){ var def = $(this).attr('data-definition'); var term = $(this).text(); var termName = $(this).attr('data-def-name'); var def = $(this).attr('data-definition'), term = $(this).text(), termName = $(this).attr('data-def-name'); $('.jargon-buster-definition:not([data-term="'+term+'"])').slideUp(); if ($('.jargon-buster-definition[data-term="'+term+'"]').length === 0) { $(this).parent('p').after( '
'+ ''+termName+': '+def+ ' close'+ '
' ); } $('.jargon-buster-definition[data-term="'+term+'"]').slideToggle(); $('.jargon-close').click(function(){ $('.jargon-buster-definition[data-term="'+term+'"]').slideUp(); }); }); }