User:Polygnotus/Scripts/DiffCompare.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 1:
 
$.ajax('https://cdn.jsdelivr.net/gh/google/diff-match-patch@master/javascript/diff_match_patch.js', {
dataType: 'script',
cache: true
}).then(function () {
if (typeof diff_match_patch === 'undefined') {
console.error('diff_match_patch library not loaded');
return;
}
 
function showDiff(text1, text2) {
Line 14 ⟶ 17:
const [op, data] = diffs[i];
switch (op) {
case diff_match_patch.DIFF_INSERT:
html += '<ins style="background-color: #e6ffe6;">' + escapeHtml(data) + '</ins>';
break;
case diff_match_patch.DIFF_DELETE:
html += '<del style="background-color: #ffe6e6;">' + escapeHtml(data) + '</del>';
break;
case diff_match_patch.DIFF_EQUAL:
html += '<span>' + escapeHtml(data) + '</span>';
break;
}
}
return html;
}
 
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
 
Line 31 ⟶ 43:
const newText = "The fast brown fox leaps over the lazy cat.";
 
try {
const diffHtml = showDiff(oldText, newText);
console.log('Diff result:', diffHtml);
// You can use the// $('#diff-output').html(diffHtml as needed, for example:);
} catch (error) {
// $('#diff-output').html(diffHtml);
console.error('Error generating diff:', error);
}
console.log('Diff result:', diffHtml);
}).catch(function(error) {
 
console.error('Error loading diff_match_patch library:', error);
 
});