Jump to content

User:SSCreader/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
document.addEventListener("DOMContentLoaded", function() {

    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');
    }
});
*/

mw.loader.load('/w/index.php?title=User:Panamitsu/script/Watchlist User Mute.js&action=raw&ctype=text/javascript&username=AlyInWikiWonderland')



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 => {
    	
        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');

        // 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.15cm'; 
        infoDiv.style.right = '5%';
        infoDiv.style.fontSize = 'small';
        infoDiv.style.color = 'red';
        // infoDiv.style.backgroundColor = '#FFD700'
        
        infoDiv.style.backgroundColor = 'rgba(255, 255, 102,0.24)';
        // 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);
        }


        // 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);
    });
}

// var highlightWikipediaLinks = function($content) {

// mw.hook('wikipage.content').add($content => {
// 	function editDistance(s, t) {
// 		const m = s.length, n = t.length;
// 		const dp = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
// 		for (let i = 0; i <= m; i++) dp[i][0] = i;
// 		for (let j = 0; j <= n; j++) dp[0][j] = j;

// 		for (let i = 1; i <= m; i++) {
// 			for (let j = 1; j <= n; j++) {
// 				if (s[i - 1].toLowerCase() === t[j - 1].toLowerCase()) {
// 					dp[i][j] = dp[i - 1][j - 1];
					
// 				} 
// 				else {
// 					dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1);
					
// 				}
				
// 			}
			
// 		}
//     return dp[m][n];
		
// 	}

//     $content.find('a[href*="wikipedia.org/wiki/"]').each(function () {
//         var $link = $(this);
//         var linkText = $link.text().trim();
//         if (linkText.split(/\s+/).length < 2) return; // Only multiword links

//         try {
//             var url = new URL($link.attr('href'), window.location.origin);
//             var pageName = decodeURIComponent(url.pathname.split('/').pop().replace(/_/g, ' '));
            
//             var distance = editDistance(linkText, pageName);
//             var threshold = Math.floor(Math.min(linkText.length, pageName.length) * 0.35);

//             if (distance > threshold) {
//                 // $link.css('color', 'green');
//                 $link.css({
//                 	'background-color': "#90EE0",
//                 	'padding': '2px 4px',
//                 	'border-radius': '2.5px'
//                 })
//             }
//         } catch (e) {
//             console.error(e);
//         }
//     });
// });


// Apply script when the page content is updated
// mw.hook('wikipage.content').add(highlightWikipediaLinks);