User:Polygnotus/Scripts/DiffCompare.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/DiffCompare.
// https://github.com/google/diff-match-patch $.ajax('https://cdn.jsdelivr.net/gh/google/diff-match-patch@master/javascript/diff_match_patch.js',{ dataType:'script', cache:true }).then(function(){ constDEBUG=false; functiondebug(...args){ if(DEBUG){ debug('[DiffCompare]',...args); } } debug('diff_match_patch library loaded successfully'); if(typeofdiff_match_patch==='undefined'){ console.error('diff_match_patch is undefined after loading'); return; } functionshowDiff(text1,text2){ debug('showDiff called with:',{text1,text2}); constdmp=newdiff_match_patch(); debug('diff_match_patch instance created'); constdiffs=dmp.diff_main(text1,text2); debug('diffs created:',diffs); dmp.diff_cleanupSemantic(diffs); debug('diffs after cleanup:',diffs); lethtml=''; for(leti=0;i<diffs.length;i++){ debug(`Processing diff ${i}:`,diffs[i]); constop=diffs[i][0]; constdata=diffs[i][1]; switch(op){ case-1:// DIFF_DELETE html+='<del style="background-color: #ffe6e6;">'+escapeHtml(data)+'</del>'; break; case1:// DIFF_INSERT html+='<ins style="background-color: #e6ffe6;">'+escapeHtml(data)+'</ins>'; break; case0:// DIFF_EQUAL html+='<span>'+escapeHtml(data)+'</span>'; break; default: console.error(`Unknown diff operation: ${op}`); } } debug('Final HTML:',html); returnhtml; } functionescapeHtml(unsafe){ returnunsafe .replace(/&/g,"&") .replace(/</g,"<") .replace(/>/g,">") .replace(/"/g,""") .replace(/'/g,"'"); } constoldText="The quick brown fox jumps over the lazy dog."; constnewText="The fast brown fox leaps over the lazy cat."; try{ debug('Attempting to generate diff'); constdiffHtml=showDiff(oldText,newText); debug('Diff generation successful'); debug('Diff result:',diffHtml); constdiffContainer=$('<div>') .attr('id','diff-output') .css({ 'border':'1px solid #a2a9b1', 'padding':'10px', 'margin-bottom':'20px', 'background-color':'#f8f9fa' }) .html('<strong>Diff Result:</strong><br>'+diffHtml); $('#bodyContent').prepend(diffContainer); }catch(error){ console.error('Error generating diff:',error); } }).catch(function(error){ console.error('Error loading diff_match_patch library:',error); });