User:SSCreader/common.js
Appearance
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. |
The accompanying .css page for this skin is at User:SSCreader/common.css. |
document.addEventListener("DOMContentLoaded", function() {
// Find the 'page views' span using its content or a parent container
var pageViewElement = document.querySelector('span:contains("page views in the last 30 days")');
if (pageViewElement) {
// Add a class to style it
pageViewElement.classList.add('pageviews');
pageViewElement.classList.add('infoDiv');
}
});
if (mw.config.get('wgNamespaceNumber') === 0 || mw.config.get('wgNamespaceNumber') === 14 || mw.config.get("wgNamespaceNumber") == 4 || mw.config.get("wgNamespaceNumber") == 2 || mw.config.get("wgNamespaceNumber") == 10 || mw.config.get("wgNamespaceNumber") == 12 || mw.config.get("wgNamespaceNumber") == 100 ) {
var pageTitle = mw.config.get('wgPageName');
// Fetch the page info from the action=info page
fetch(`/w/index.php?title=${pageTitle}&action=info`)
.then(response => response.text())
.then(data => {
// Parse the response HTML to extract page creation date and page views
const parser = new DOMParser();
const doc = parser.parseFromString(data, 'text/html');
const creationDateElement = doc.querySelector('#mw-pageinfo-firsttime td:nth-child(2) a');
const pageViewsElement = doc.querySelector('#mw-pvi-month-count td:nth-child(2) a');
const totalEditsElement = doc.querySelector('#mw-pageinfo-edits td:nth-child(2)');
// Create a new div to display the creation date and page views
const infoDiv = document.createElement('div');
infoDiv.style.position = 'absolute';
infoDiv.style.top = '2.35cm';
infoDiv.style.right = '5%';
infoDiv.style.fontSize = 'small';
infoDiv.style.color = 'red';
// infoDiv.style.backgroundColor = rgb(189, 132, 144);
// infoDiv.style.color = '#FF0000';
// infoDiv.style.color = '#663399'; // purple
// Add creation date if the element exists
if (creationDateElement) {
const creationDate = creationDateElement.textContent.trim();
infoDiv.textContent = `Page created: ${creationDate}`;
}
// Add page views if the element exists, place it under the creation date
if (pageViewsElement) {
const pageViews = pageViewsElement.textContent.trim();
const pageViewsText = document.createElement('div');
pageViewsText.textContent = `Page views (last 30 days): ${pageViews}`;
infoDiv.appendChild(pageViewsText);
}
if (totalEditsElement) {
// let total_edits_string = totalEditsElement.textContent.trim();
const total_edits_div = document.createElement("div")
total_edits_div.textContent = "Total edits:" + "\t"+"\t" +totalEditsElement.textContent.trim()
infoDiv.appendChild(total_edits_div)
}
// Append the div to the body or content area of the article
document.body.appendChild(infoDiv);
})
.catch(error => {
console.error('Error fetching page info:', error);
});
}