Enhance

Enlarge all p-Tags on a page with AltGr+p, reduce with AltGr+l, reset with AltGr-o. ALtGr+b sets all p-Tags to have black text and a white background.

As of 24/02/2021. See the latest version.

// ==UserScript==
// @name        Enhance
// @namespace   meyerk.com
// @match       *://*/*
// @grant       none
// @version     1.4
// @author      MeyerK
// @description Enlarge all p-Tags on a page with AltGr+p, reduce with AltGr+l, reset with AltGr-o. ALtGr+b sets all p-Tags to have black text and a white background.
// ==/UserScript==

class enhance
{
  constructor()
  {
    this.rightAltKeyIsOn = false;
    this.zoomInc = 3;
    this.currentZoomStep = 0;
    this.maxZoomSteps = 7;
  }
  
  toggleAltGr(ev)
  {
    if (ev.code == 'AltRight')
    {
      this.rightAltKeyIsOn = (ev.type == 'keydown') ? true : false; 
    }    
  }
  
  do(ev)
  {        
    if (this.rightAltKeyIsOn)
    {
      if (ev.code == 'KeyP')
      {
        if (this.currentZoomStep < this.maxZoomSteps)
        {
          this.currentZoomStep = this.currentZoomStep + 1;
          this.setFontSize('inc');
        }
        else
        {
          this.currentZoomStep = this.maxZoomSteps;
        }
      }

      if (ev.code == 'KeyL')
      {
        if (this.currentZoomStep > 0)
        {
          this.currentZoomStep = this.currentZoomStep - 1;
          this.setFontSize('dec');
        }
        else
        {
          this.currentZoomStep = 0;
        }        
      }
            
      if (ev.code == 'KeyO')
      {
        this.currentZoomStep = 0;
        this.resetFontSize();
      }
      
      if (ev.code == 'KeyB')
      {
        this.setPropertyOfPs('color', 'black');
        this.setPropertyOfPs('backgroundColor', 'white');
      }
    }
  }
    
  setFontSize(act)
  {
    let newSize = null;
    let currentSize = null;
    let pElems = document.querySelectorAll('p');
    
    pElems.forEach((pElem) =>
    {
      currentSize = parseInt(window.getComputedStyle(pElem).fontSize, 10);
            
      if (this.currentZoomStep == 0)
      {        
        newSize = '';
      }
      else
      {
        if (act == 'inc')
        {
          newSize = (currentSize + this.zoomInc) + 'px';
        }
        else if (act == 'dec')
        {
          newSize = (currentSize - this.zoomInc) + 'px';          
        }
      }
      
      pElem.style.fontSize = newSize;
      
    });
  }
  
  resetFontSize()
  {
    this.setPropertyOfPs('fontSize', '');
  }
    
  setPropertyOfPs(propName, val)
  {
    var i = 0;
    var pElems = null;
    
    pElems = document.querySelectorAll('p');
    pElems.forEach((pElem) => 
    {
      pElem.style[propName] = val;
    });
  }
}

var e = new enhance();
document.addEventListener('keydown', e.toggleAltGr.bind(e));
document.addEventListener('keyup',   e.toggleAltGr.bind(e));
document.addEventListener('keyup',   e.do.bind(e));
长期地址
遇到问题?请前往 GitHub 提 Issues,或加Q群1031348184

赞助商

Fishcpy

广告

Rainyun

注册一下就行

Rainyun

一年攒够 12 元