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

Content deleted Content added
sync from User:SD0001/comments in local time.js - requested on talk
Tag: Reverted
fixes
 
(3 intermediate revisions by 2 users not shown)
Line 1:
// Forked from [[User:Gary/comments in local time.js]]
/**
* COMMENTS IN LOCAL TIME
Line 10 ⟶ 9:
* [[Wikipedia:Comments in Local Time]]
*/
$(() => {
mw.hook('wikipage.content').add(($content) => {
/**
* Given a number, add a leading zero if necessary, so that the final number
Line 132 ⟶ 131:
const minute = addLeadingZero(time.getMinutes());
const finalTime = `${hour}:${minute}${ampm}`;
let returnDate;
 
// Determine the time offset.
Line 139 ⟶ 137:
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
 
const returnDateutcPart = `${finalTime}, ${date} (UTC${utcOffset})`;
if (this.LocalComments.timeFirst) {
 
returnDate = `${finalTime}, ${date} (UTC${utcOffset})`;
ifconst returnDate = (this.LocalComments.timeFirst) {
} else {
returnDate =? `${datefinalTime}, ${finalTimedate} (UTC${utcOffsetutcPart})`;
: `${date}, ${finalTime} ${utcPart}`;
}
 
return { returnDate, time };
}
 
Line 206 ⟶ 204:
}
 
letconst formattedDayOfTheWeek = '';this.LocalComments.dayOfWeek
formattedDayOfTheWeek =? `, ${last}${dayOfTheWeek}`;
: )'';
 
return `${formattedDate + }${formattedDayOfTheWeek + }${descriptiveDifference}`;
if (this.LocalComments.dayOfWeek) {
formattedDayOfTheWeek = `, ${last}${dayOfTheWeek}`;
}
 
return formattedDate + formattedDayOfTheWeek + descriptiveDifference;
}
 
Line 367 ⟶ 363:
getHour(time) {
let ampm;
let hour = Number.parseInt(time.getHours(), 10);
 
if (this.LocalComments.twentyFourHours) {
Line 417 ⟶ 413:
// 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 428 ⟶ 421:
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.substring(0, position);
const afterMatch = value.substring(position + stringLength);
const { returnDate, time } = this.adjustTime(
match.toString(),
search
);
const timestamp = time ? time.getTime() : '';
 
// IsGrab the "timestamp"text attributecontent usedbefore forand microformats?after the matching timestamp,
// which we'll then wrap in their own SPAN nodes.
const span = document.createElement('span');
const beforeMatch = value.substringslice(0, position);
const afterMatch = value.substringslice(position + stringLength);
const { returnDate, time } = this.adjustTime(match.toString(), search);
 
// Create the code to display the new local comments content.
span.className = 'localcomments';
const $span.style.fontSize = '95%';$(
`<span class="localcomments" style="font-size: 95%;" title="${match}">${returnDate}</span>`
span.style.whiteSpace = 'nowrap';
span.setAttribute('timestamp', timestamp);
span.title = match;
span.append(document.createTextNode(returnDate));
 
parent// =Replace the existing text node.parentNode; in the page with our new local
parent.replaceChild(span,// comments node);.
$(node).replaceWith($span);
 
const// Replace the text content that appears before =the documenttimestamp.createElement('span');
if (beforeMatch) {
match$span.toStringbefore(),
`<span class="before-localcomments">${beforeMatch}</span>`
search);
} else { }
 
// Replace the text content that appears after the timestamp.
before.className = 'before-localcomments';
if (afterMatch) {
before.append(document.createTextNode(beforeMatch));
span.title = match;$span.after(
 
const after = document.createElement(' `<span class="after-localcomments">${afterMatch}</span');>`
return );
 
}
after.className = 'after-localcomments';
after.append(document.createTextNode(afterMatch));
 
parent.insertBefore(before, span);
parent.insertBefore(after, span.nextSibling);
}
} else {
Line 504 ⟶ 491:
 
if (isDisabledUrl) {
return;
}
// If this content already contains localcomments class, it was
// already worked on.
if ($content[0].querySelector('.localcomments')) {
return;
}
 
this.replaceText(
$document.querySelector('.mw-body-content[0] .mw-parser-output'),
/(\d{1,2}):(\d{2}), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/
);
Line 625 ⟶ 606:
}
 
// Check if we've already ran this script.
new CommentsInLocalTime().run();
if (window.commentsInLocalTimeWasRun) {
return;
}
 
window.commentsInLocalTimeWasRun = true;
 
const commentsInLocalTime = new CommentsInLocalTime().run();
 
commentsInLocalTime.run();
});