NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Keywords Highlight // @namespace andrey y0u petrov // @version 1.0 // @description Keywords Highlight // @author andrew y0u petrov // @match *://*/* // @require http://code.jquery.com/jquery-latest.js // ==/UserScript== $.extend({ highlight: function (node, re, nodeName, className) { if (node.nodeType === 3) { var match = node.data.match(re); if (match) { var highlight = document.createElement(nodeName || 'span'); highlight.className = className || 'highlight'; var wordNode = node.splitText(match.index); wordNode.splitText(match[0].length); var wordClone = wordNode.cloneNode(true); highlight.appendChild(wordClone); wordNode.parentNode.replaceChild(highlight, wordNode); return 1; //skip added node in parent } } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children !/(script|style)/i.test(node.tagName) && // ignore script and style nodes !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted for (var i = 0; i < node.childNodes.length; i++) { i += jQuery.highlight(node.childNodes[i], re, nodeName, className); } } return 0; } }); $.fn.unhighlight = function (options) { var settings = { className: 'highlight', element: 'span' }; jQuery.extend(settings, options); return this.find(settings.element + "." + settings.className).each(function () { var parent = this.parentNode; parent.replaceChild(this.firstChild, this); parent.normalize(); }).end(); }; $.fn.highlight = function (words, options) { var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false }; jQuery.extend(settings, options); if (words.constructor === String) { words = [words]; } words = jQuery.grep(words, function(word, i){ return word != ''; }); words = jQuery.map(words, function(word, i) { return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); }); if (words.length == 0) { return this; }; var flag = settings.caseSensitive ? "" : "i"; var pattern = "(" + words.join("|") + ")"; if (settings.wordsOnly) { pattern = "\\b" + pattern + "\\b"; } var re = new RegExp(pattern, flag); return this.each(function () { jQuery.highlight(this, re, settings.element, settings.className); }); }; $(document).ready(function(){ $('body').append('<div id="keyword-wrap"><div id="showclose-key">Показать/Скрыть</div><div id="kkkqqq"><textarea id="KeywordsHighLight"></textarea><button id="find-key">Поиск</button><button id="del-key">Удалить</button></div></div>'); $('head').append('<style type="text/css">.highlight{background: green; color: #fff;} #keyword-wrap{position: fixed; top: 20%; z-index: 9999999; right: 0; width: 200px; min-height: 200px;padding: 15px;} #showclose-key{background: green; color: #fff; padding: 10px; text-align: center;} #showclose-key:hover{cursor: pointer;}#kkkqqq{background: #AFAFAF; padding: 10px;} #KeywordsHighLight{width: 100%; max-width:200px; height: 150px} #keyword-wrap button{padding: 10px; text-align: center; background: #9B2A2A; color: #fff; outline: none; border: none; margin-right: 10px; border-radius: 5px;}</style>'); $('#find-key').click(function(){ var a = $('#KeywordsHighLight').val().split('\n'); $('body').highlight(a); }); $('#del-key').click(function(){ $('body').unhighlight(); }); $('#showclose-key').click(function(){ $('#kkkqqq').slideToggle(); }); });