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.
var pg = mw.config.get('wgPageName');

fetch(`/w/index.php?title=${pg}&action=info`)
  .then(response => response.text())
  .then(data => {

    const parser = new DOMParser();
    const doc = parser.parseFromString(data, 'text/html');
    function getWordCountLabel(wordCount) {
      if (wordCount <= 999) return 'Very short'
      if (wordCount >= 1000 && wordCount <= 2500) return 'Short';
      if (wordCount >= 2501 && wordCount <= 5500) return 'Medium';
      if (wordCount >= 5500 && wordCount <= 9500) return 'Long';
      if (wordCount >= 9500) return 'Very long'
      return "fail"

    }
    function getWordCount() {
      var text = document.body.innerText || '';
      let words = text.trim().split(/\s+/).filter(Boolean);
      let ref_idx = words.lastIndexOf("References[edit")
      words = words.slice(0, ref_idx)
      return words.length;
    }
    var creationDateElement = doc.querySelector('#mw-pageinfo-firsttime td:nth-child(2) a');
    var pageViewsElement = doc.querySelector('#mw-pvi-month-count td:nth-child(2) a');
    // var pageViewsElement = doc.querySelector('#mw-pvi-month-count');

    var infoDiv = document.createElement('div');
    infoDiv.style.position = 'absolute';
    infoDiv.style.top = "2.175cm"
    infoDiv.style.right = '5%';
    infoDiv.style.fontSize = 'small';
    infoDiv.style.backgroundColor = '#fff8e1'; // Soft light ivory
    infoDiv.style.color = '#cc0000'; // Deep, saturated red
    infoDiv.style.boxShadow = '0 4px 16px rgba(0, 0, 0, 0.25)';

    infoDiv.style.borderRadius = '7.5px';
    infoDiv.style.border = '1.2px solid black';
    infoDiv.style.lineHeight = '1.4';
    infoDiv.style.padding = '9px 12px';

    // infoDiv.style.fontWeight = '900';
    // infoDiv.style.border = '2px solid rgba(255, 255, 255, 0.2)';

    // infoDiv.style.color = 'red';  
    // infoDiv.style.color = '#B22222'; // lighter red

    // infoDiv.style.borderRadius = '12px';
    // infoDiv.style.backgroundColor = '#FFFACD'; // lighter yellow 
    // infoDiv.style.backgroundColor = '#FFD700' // yellow

    // infoDiv.style.backgroundColor = '#FFD700';    // Strong gold
    // infoDiv.style.textShadow = '1px 1px 2px rgba(0,0,0,0.4)'; // Soft black shadow for legibility
    // infoDiv.style.color = '#c60000';  // Richer red
    // infoDiv.style.boxShadow = '0 4px 10px rgba(0, 0, 0, 0.3)'; // Clean lift

    // infoDiv.style.boxShadow = '0 2px 6px rgba(0, 0, 0, 0.2)';
    // infoDiv.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.3)'; // darker shadow

    // infoDiv.style.border = '2.5px solid green'; 
    // infoDiv.style.borderRadius = '10px'; // rounded border

    // 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) {
      let creationDate = creationDateElement.textContent.trim();
      creationDate = creationDate.split(" ").slice(1)
      creationDate[1] = creationDate[1].slice(0, 3)
      let final_string = creationDate.join(" ")
      infoDiv.textContent = `Page created: ` + final_string
    }

    // Add page views if the element exists, place it under the creation date
    if (pageViewsElement) {
      var pageViews = pageViewsElement.textContent.trim();
      var pageViewsText = document.createElement('div');
      // pageViewsText.textContent = `Page views: ${pageViews}`;
      let spaces = "  "
      let spaces2 = spaces.repeat(2)

      pageViewsText.textContent = `Pageviews (30 days): ${pageViews}`
      infoDiv.appendChild(pageViewsText);
    }

    var wordDiv = document.createElement("div")
    let wordCount = getWordCount()
    let short_medium_long = getWordCountLabel(wordCount)

    // wordDiv.textContent = "Word count: " + wordCount + " (" + short_medium_long + ")"
    // infoDiv.appendChild(wordDiv)


    // 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);
  });
mw.loader.load('/w/index.php?title=User:Panamitsu/script/Watchlist User Mute.js&action=raw&ctype=text/javascript&username=AlyInWikiWonderland')

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

importScript('User:SSCreader/global.js');