User:Gary/comments in local time.js: Difference between revisions

Content deleted Content added
lint
bug fixes
Line 10:
*/
$(() => {
const nowrapStyle = `style="white-space: nowrap;"`;
 
/**
* Given a number, add a leading zero if necessary, so that the final number
Line 136 ⟶ 138:
const utcOffset =
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
 
const timePart = `<span class="localcomments-time" ${nowrapStyle}>${finalTime}</span>`;
const datePart = `<span class="localcomments-date">${date}</span>`;
const utcPart = `<span class="localcomments-utc" ${nowrapStyle}>(UTC${utcOffset})</span>`;
 
const returnDate = this.LocalComments.timeFirst
? `${finalTimetimePart}, ${datedatePart} (UTC${utcOffsetutcPart})`
: `${datedatePart}, ${finalTimetimePart} (UTC${utcOffsetutcPart})`;
 
return { returnDate, time };
Line 202 ⟶ 208:
}
 
letconst formattedDayOfTheWeek = '';this.LocalComments.dayOfWeek
formattedDayOfTheWeek =? `, ${last}${dayOfTheWeek}`;
} : '';
 
return `<span ${nowrapStyle}>${formattedDate}</span><span + ${nowrapStyle}>${formattedDayOfTheWeek}</span><span + ${nowrapStyle}>${descriptiveDifference}</span>`;
if (this.LocalComments.dayOfWeek) {
formattedDayOfTheWeek = `, ${last}${dayOfTheWeek}`;
}
 
return formattedDate + formattedDayOfTheWeek + descriptiveDifference;
}
 
Line 413 ⟶ 417:
// Check if this is a text node.
if (node.nodeType === 3) {
// Don't continue if this text node's parent tag is one of these.
let parent = node.parentNode;
if (['CODE', 'PRE'].includes(parentNodeNamenode.parentNode.nodeName)) {
 
const parentNodeName = parent.nodeName;
 
if (['CODE', 'PRE'].includes(parentNodeName)) {
return;
}
Line 424 ⟶ 425:
const matches = value.match(search);
 
// Stick with manipulating the DOM directly rather than using jQuery.
// I've got more than a 100% speed improvement afterward.
if (matches) {
// Only act on the first timestamp we found in this node. This is for
// reallythe arare temporaryoccassion fixthat forthere theis situationmore inthan whichone theretimestamp arein two orthe
// moresame timestamps in the sametext node.
const [match] = matches;
const position = value.search(search);
const stringLength = match.toString().length;
 
const beforeMatch = value.slice(0, Math.max(0, position));
// Grab the text content before and after the matching timestamp,
const afterMatch = value.slice(Math.max(0, position + stringLength));
// which we'll then wrap in their own SPAN nodes.
const beforeMatch = value.slice(0, Math.max(0, position));
const afterMatch = value.slice(Math.max(0, position + stringLength));
const { returnDate, time } = this.adjustTime(
match.toString(),
Line 442 ⟶ 444:
 
// Is the "timestamp" attribute used for microformats?
const $span = document.createElement$('span');
`<span class="localcomments" style="font-size: 95%;" timestamp="${timestamp}" title="${match}">${returnDate}</span>`
);
 
span$(node).className = 'localcomments'replaceWith($span);
span.style.fontSize = '95%';
span.style.whiteSpace = 'nowrap';
span.setAttribute('timestamp', timestamp);
span.title = match;
span.append(document.createTextNode(returnDate));
 
parentconst $before = node.parentNode;$(
`<span class="before-localcomments">${beforeMatch}</span>`
node.replaceWith(span);
);
 
const before$after = document.createElement$('span');
`<span class="after-localcomments">${afterMatch}</span>`
 
before.className = 'before-localcomments');
before.append(document.createTextNode(beforeMatch));
 
const after = document.createElement('span');
 
after.className = 'after-localcomments';
after.append(document.createTextNode(afterMatch));
 
$span.before($before);
parent$span.insertBeforeafter($after, span.nextSibling);
}
} else {
Line 504 ⟶ 498:
 
this.replaceText(
document.querySelector('.mw-body-content .mw-parser-output'),
/(\d{1,2}):(\d{2}), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/
);