NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Comrademao - Entity Colorizer // @namespace comcolorizer // @match https://comrademao.com/mtl/*/*/ // @grant GM_xmlhttpRequest // @grant GM_addStyle // @version 1.0 // @license MIT // @author Spawner // @description 3/18/2020, 16:10:42 AM // @require http://code.jquery.com/jquery-3.4.1.min.js // ==/UserScript== /* ChangeLog 1.0 - Initial Release */ GM_addStyle("hgltr { color:mediumpurple }"); const colorName = "mediumpurple"; const colorLocation = "tomato"; let hashTableWords; window.onload = function() { hashTableWords = new Map(); let getText = document.getElementById('content').innerText; runScript(getText); }; function runScript(text) { GM_xmlhttpRequest( { method : "POST", url : "https://api.explosion.ai/displacy/ent", data : JSON.stringify( { "model" : "en_core_web_md", "text" : text }), headers : { "Content-Type": "application/json;charset=UTF-8" }, onload: function(response) { let jsonArray = JSON.parse(response.responseText); jsonArray.forEach(function(obj) { let entityType = obj.label; if( entityType == "PERSON" || entityType == "LOC" ) { let entityData = text.slice( obj.start, obj.end ); hashTableWords.set( entityData, entityType ); } }); for (const [key, value] of hashTableWords.entries()) { let color = ( value[0] == "P" ) ? colorName : colorLocation; $('#content').html( $('#content').html().replace ( new RegExp( '\\b(' + key + ')\\b', 'gi' ), '<hgltr style="color:' + color + '">' + key + '</hgltr>' ) ); } } }); }