Greasy Fork is available in English.

Neil Fraser Diff Demo - Enhanced Output View

Easier to compare between corrections, especially for Japanese.

  1. // ==UserScript==
  2. // @name Neil Fraser Diff Demo - Enhanced Output View
  3. // @namespace NFDiff_KK
  4. // @author Kai Krause <kaikrause95@gmail.com>
  5. // @description Easier to compare between corrections, especially for Japanese.
  6. // @include http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
  7. // @include https://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
  8. // @include https://neil.fraser.name/software/diff_match_patch/demos/diff.html
  9. // @version 1.3
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. //Allow selecting text for copying. Source: http://www.javascriptkit.com/javatutors/copytoclipboard.shtml
  14. var script = document.createElement("script");
  15. script.type = "text/javascript";
  16. script.innerHTML = 'function clearComparison(){text1.value = ""; text2.value = "";}; function startSelect(el){var originalText = document.getElementById("original"); var correctedText = document.getElementById("correction"); if (el == 1){selectElementText(originalText);} else {selectElementText(correctedText)}}; function selectElementText(el){var range = document.createRange(); range.selectNodeContents(el); var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range);}';
  17. document.head.appendChild(script);
  18.  
  19. //Reset text comparison
  20. text1.value = "";
  21. text2.value = "";
  22. var inputArea = document.getElementsByTagName('tbody');
  23. inputArea[0].innerHTML += '<button type="button" onclick="clearComparison()">Clear Comparison</button>';
  24.  
  25. document.getElementsByTagName('input')[5].addEventListener('click', function() {
  26. //Get output
  27. var outputdiv = document.getElementById('outputdiv');
  28. var output = outputdiv.getElementsByTagName('*');
  29. //Sort output
  30. var original = '';
  31. var correction = '';
  32. for (var i = 0; i < output.length; i++){
  33. if (output[i].tagName == "SPAN"){
  34. original += output[i].outerHTML;
  35. correction += output[i].outerHTML;
  36. }
  37. else if (output[i].tagName == "DEL"){
  38. original += output[i].outerHTML;
  39. }
  40. else if (output[i].tagName == "INS"){
  41. correction += output[i].outerHTML;
  42. }
  43. }
  44. //Replace highlight with text color (for better copying into word docs, etc)
  45. original = original.replace(/style="background:#ffe6e6;"/igm, 'style="color:red;"');
  46. original = original.replace(/¶/igm, '');
  47. original = original.replace(/<del/igm, '<span');
  48. original = original.replace(/del>/igm, 'span>');
  49. original = original.replace(/<br>/igm, '<div><p></p></div>');
  50. correction = correction.replace(/style="background:#e6ffe6;"/igm, 'style="color:green;"');
  51. correction = correction.replace(/¶/igm, '');
  52. correction = correction.replace(/<ins/igm, '<span');
  53. correction = correction.replace(/ins>/igm, 'span>');
  54. correction = correction.replace(/<br>/igm, '<div><p></p></div>');
  55. //Rewrite output
  56. outputdiv.innerHTML = '<table border="1" cellspacing="5" cellpadding="5" style="width:100%; border-collapse: collapse;"><tr><td style="width:49.9%; display:inline-table" id="original">' + original + '</td><td style="width:49.9%; display:inline-table" id="correction">' + correction + '</td></tr></table>';
  57. outputdiv.innerHTML += '<table border="0" style="width:100%"><tr><td style="width:49.9%"><button type="button" onclick="startSelect(1)">Select All</button></td><td style="width:49.9%"><button type="button" onclick="startSelect(2)">Select All</button></td></tr></table>';
  58. }, false);