User:WikiMacaroons/testrec.js

if(window.location.href == "https://en.wikipedia.org/wiki/User:WikiMacaroons/C"){ document.querySelector('html').innerHTML = "                                                  "; count = 0; //var EventSource = require('eventsource'); var url = 'https://stream.wikimedia.org/v2/stream/recentchange'; console.log(`Connecting to EventStreams at ${url}`); var eventSource = new EventSource(url); eventSource.onopen = function(event) { console.log('--- Opened connection.'); };	eventSource.onerror = function(event) { console.error('--- Encountered error', event); };	eventSource.onmessage = function(event) { // event.data will be a JSON string containing the message event. //console.log(JSON.parse(event.data)); var change = JSON.parse(event.data); if(change.wiki == "enwiki"){ console.log(JSON.parse(event.data)); revnew = change.revision.new; revold = change.revision.old; var params = { action: "compare", format: "json", fromrev: revold, torev: revnew },			api = new mw.Api; api.get( params ).done( function ( diff ) {				table = diff.compare["*"];				document.getElementById('table').innerHTML = table;				var diffin = table.indexOf("\"diff-addedline\"");				subd = table.substring(diffin + 17, table.length);				subdcontent = subd.substring(subd.indexOf(" ") + 5, subd.indexOf(" "));				toxicCheck(subdcontent, revnew);			} ); }	}; }

function toxicCheck(pvandalism, rdiff){ const threshold = 0.9;

// Load the model. Users optionally pass in a threshold and an array of // labels to include. toxicity.load(threshold).then(model => { //pvandalism = document.getElementById("demo").textContent  document.getElementById("demo").innerHTML = pvandalism;  alert(document.getElementById("demo").rows[0].cells[0].namedItem("diff-addedline").innerHTML);  const sentences = [pvandalism];

model.classify(sentences).then(predictions => {   // `predictions` is an array of objects, one for each prediction head,    // that contains the raw probabilities for each input along with the    // final prediction in `match` (either `true` or `false`).    // If neither prediction exceeds the threshold, `match` is `null`.

console.log(predictions); var newstr = JSON.stringify(predictions); var newjson = JSON.parse(newstr); var tags = []; if(JSON.stringify(newjson[0].results[0].match) == "true"){ tags.push("identity_attack"); }   if(JSON.stringify(newjson[1].results[0].match) == "true"){ tags.push("insult"); }   if(JSON.stringify(newjson[2].results[0].match) == "true"){ tags.push("obscene"); }   if(JSON.stringify(newjson[3].results[0].match) == "true"){ tags.push("severe_toxicity"); }   if(JSON.stringify(newjson[4].results[0].match) == "true"){ tags.push("sexual_explicit"); }   if(JSON.stringify(newjson[5].results[0].match) == "true"){ tags.push("threat"); }   if(JSON.stringify(newjson[6].results[0].match) == "true"){ tags.push("toxicity"); }   if(tags.length > 0){ alert("Vandalism identified at "+rdiff); } }); }); }