跳转到内容

User:SSCreader/common.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
var pageTitle = mw.config.get('wgPageName');

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

    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 "fn failed"
    }
    
    function getWordCount() {
      const text = document.body.innerText || '';

      let words = text.trim().split(/\s+/).filter(Boolean);
      let ref_idx = words.lastIndexOf("Referencias[editar")
      console.log("ref_idx: " + ref_idx)
      console.log(words)

      words = words.slice(0, ref_idx)
      return words.length;
    }

    const creationDateElement = doc.querySelector('#mw-pageinfo-firsttime td:nth-child(2) a');
    var pageViewsElement = doc.querySelector('#mw-pvi-month-count td:nth-child(2) .mw-pvi-month');
    //console.log(pageViewsElement);
    //console.log(doc.querySelector('#mw-pvi-month-count'));
    //console.log(doc.querySelector('#mw-pvi-month-count').innerHTML);

    const infoDiv = document.createElement('div');
    infoDiv.style.position = 'absolute';
    infoDiv.style.top = '2.25cm';
    infoDiv.style.right = '5%';		
    infoDiv.style.fontSize = '14px';
	infoDiv.style.pointerEvents = 'none'
    infoDiv.style.fontWeight = '600';
    infoDiv.style.lineHeight = '1.6';
    infoDiv.style.padding = '12px 16px';
    infoDiv.style.color = '#fefefe'; // near white text
    infoDiv.style.backgroundColor = 'rgba(17, 17, 17, 0.6' // style 2
    infoDiv.style.border = '1.5px solid #22c55e'; //style 2
    infoDiv.style.borderRadius = '12px';
    
    infoDiv.style.boxShadow = '0 0 10px rgba(34, 197, 94, 0.3)'; // style 2
    
    infoDiv.style.zIndex = '9999';
    infoDiv.style.maxWidth = '320px';
    
    var creationDate = creationDateElement.textContent.trim();
    infoDiv.textContent = creationDate
    // if (creationDateElement) {
    //   var creationDate = creationDateElement.textContent.trim();
    //   creationDate = creationDate.split(" ").slice(1)
    //   creationDate[1] = creationDate.at(1).at(0).toUpperCase()
    //     + creationDate.at(1).slice(1)
    //   let final_string = creationDate.join(" ")
    //   infoDiv.textContent = `Page created: ` + final_string
    //   // infoDiv.textContent = `Page created: ${final_string}`;
    // }

    if (pageViewsElement) {
      const pageViews = pageViewsElement.textContent.trim();
      const pageViewsText = document.createElement('div');
      pageViewsText.textContent = `Page views (last month): ${pageViews}`;
      infoDiv.appendChild(pageViewsText);
    }

    const wordDiv = document.createElement("div")

    let wordCount = getWordCount()
    let short_medium_long = getWordCountLabel(wordCount)

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

    document.body.appendChild(infoDiv);
  })
  .catch(error => {
    console.error('Error fetching page info:', error);
  });