mini clock 2020figuccio

clock con comandi multiposizione e mostra nascondi ora dimensione font personalizzabile dal menu

目前為 2020-06-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name        mini clock 2020figuccio
// @namespace https://greasyfork.org/users/237458
// @description clock con comandi multiposizione e mostra nascondi ora dimensione font personalizzabile dal menu
// @version     1.4
// @include     *
// @noframes
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_registerMenuCommand
// @icon        data:image/gif;base64,R0lGODlhEAAQAKECABEREe7u7v///////yH5BAEKAAIALAAAAAAQABAAAAIplI+py30Bo5wB2IvzrXDvaoFcCIBeeXaeSY4tibqxSWt2RuWRw/e+UQAAOw==
// ==/UserScript==
     /* riga 195  width:65px;al posto di auto  */
 "use strict";
var doc = document, isTop = window.self==window.top, isFF = /Firefox/i.test(navigator.userAgent),
  div, spn, wrapper,
  ref = 0, fontSize,
  hTimer = 0, hTimer2 = 0, hTimer3 = 0,
  datePattern = (function(){
    switch(navigator.language.slice(0,2)){
    }
  }());


function getValue(name, dflt) { return (typeof(GM_getValue)=='function') ? GM_getValue(name, dflt) : dflt; }
function setValue(name, value) { if(typeof(GM_setValue)=='function') GM_setValue(name, value); }
function addStyle(style) { var o = obj('+STYLE', doc.getElementsByTagName('HEAD')[0]); o.innerHTML = style; return o; }

function obj(name, parent) {
  if(!parent) parent = doc;
  switch (name.charAt(0)) {
  case '#':
    return parent.getElementById(name.slice(1));
  case '.':
    return parent.getElementsByClassName(name.slice(1))[0];
  case '+':
    var a = name.split(','); name = a.shift();
    var m = name.match(/^\+([A-Za-z]+)\b/), node = doc.createElement(m[1]);
    m = name.match(/\.\w+/); if(m) node.className = m[0].slice(1);
    m = name.match(/#\w+/); if(m) node.id = m[0].slice(1);
    while(a.length) {
      var l = a.shift().split('=');
      if(!l[1]) l.push(l[0]);
      else if(/^".*"$/.test(l[1])) l[1] = l[1].slice(1,-1);
      switch(l[0]){
      case '': node.textContent = l[1]; break;
      case 'HTML': node.innerHTML = l[1]; break;
      default: node.setAttribute(l[0], l[1]);
      }
    };
    if(parent != doc) parent.appendChild(node);
    return node;
  }
  return parent.getElementsByTagName(name)[0];
}
function miniClockSetWidth() {
  GM_setValue('miniClockMinWidth', window.outerWidth);
  alert('New treshold width is '+window.outerWidth+' pixel.');
}


function niceTime(date) { return date.toLocaleTimeString('it' ,{
      });} //ore minuti e secondi data

function niceDate(d) { return d.toLocaleDateString('it' ,{day:'2-digit',month:'long',weekday:'long',year:'numeric',});}//mostra data lingua italiana menu contest (short long)


if(isTop) {
  if(typeof(GM_registerMenuCommand)=='function') {

    if(!isFF) {
GM_registerMenuCommand('nascondi/mostra mini clock', function(event) {div.style.display = ((div.style.display!='none') ? 'none' : 'block');})
GM_registerMenuCommand('cambia dimensioni font clock ', FontChange, 's');
    }
  }
  init();
}
//serve per cambiare i font
function FullScreenElement() {
  if(doc.exitFullscreen) return doc.fullscreenElement;
  if(doc.mozCancelFullScreen) return doc.mozFullScreenElement;
  if(doc.webkitExitFullscreen) return doc.webkitFullscreenElement;
}
//serve per cambiare i font
function OnFullScreenChange(fn) {
  if(doc.exitFullscreen) doc.addEventListener("fullscreenchange", fn);
  if(doc.mozCancelFullScreen) doc.addEventListener("mozfullscreenchange", fn);
  if(doc.webkitExitFullscreen) doc.addEventListener("webkitfullscreenchange", fn);
}

if(isFF) OnFullScreenChange(function(event) {
 try {
  if(!ref++)
    init();
  ref--;
 } catch(e) { console.log(e); }
});

function OnTimer() {
  var dt = new Date(), t = niceTime(dt);
  if(spn.textContent != t) {
    spn.textContent = t;
    if(!div.title || t.slice(-1)=='0')
      div.title = niceDate(dt);
  }
}


function shouldShow() {
  if(isTop) {
    var minWidth = getValue('miniClockMinWidth', 0);
    if(!minWidth || minWidth >= window.screen.availWidth || window.outerWidth >= minWidth)
      return true;
  } else
    return isFullScreen();
  return false;
}

function OnReSize() {
  try {
    clearInterval(hTimer);
    if(shouldShow()) {
      OnTimer();
      div.style.display = 'block';
      hTimer = setInterval(OnTimer, 70);//ok
    } else {
      div.style.display = 'none';
      hTimer = 0;
    }
    if(wrapper)
      OnMouseMove();
  } catch(e) { console.log(e); }
}


function InsertClock(here) {
  if(div) {
    div.parentNode.removeChild(div);
    here.appendChild(div);
  }
}

function FontChange(event) {
  var frm = obj('+FORM,style=position:fixed!important;top:55px;right:10px;color:#222;background-color:#181818;border:1px solid gray;padding:10px 17px;font:18pt normal sans-serif;z-index:2147483647;'),
    inp = obj('+INPUT#us_MiniClockFontSel,type=number,min=3,max=200,maxlength=3,size=3,style=text-align:right;width:84px;,value='+fontSize, frm),
    btn = obj('+BUTTON,type=submit,= OK ', frm),
    fse = FullScreenElement() || doc.body;
  inp.onchange = function(event) {
    div.style.fontSize = inp.value + 'pt';
    div.style.visibility = '';
  };
  btn.onclick = function(event) {
    setValue('fontsize', fontSize = inp.value);
    frm.parentNode.removeChild(frm);
    return false;
  };
  fse.appendChild(frm);
  inp.focus();
}

function MenuCreate(menu) {
  obj('+MENUITEM,label=hide mini clock', menu).onclick = function(event) { div.style.opacity='0'; };
  obj('+MENUITEM,label=adjust clock size', menu).onclick = FontChange;
  var item = obj('+MENUITEM,label=clock at bottom,type=checkbox', menu);
  item.onclick = AtBottomClicked;
  if(getValue('atbottom'))
    item.checked = true;
  //if(isFF) {
  item = obj('+MENUITEM,label=allow clock in video tag,type=checkbox', menu);
  item.onclick = function() { setValue('allowAtVideo', !getValue('allowAtVideo')); };
  if(getValue('allowAtVideo'))
    item.checked = true;
  //}
  div.appendChild(menu);
  div.setAttribute('contextmenu','us_MiniClockMenu');
  div.oncontextmenu = function(event) {
    menu.lastChild.checked = getValue('allowAtVideo')==true;
    CancelAutoHide(event);
  };
}


function create() {
  div = obj('+DIV#us_MiniClock');
  spn = obj('+SPAN', div);
  if(getValue('atbottom')) {
    div.style.top = 'unset';
    div.style.bottom = '0px';
  }
  doc.body.appendChild(div);
  OnReSize();
  if(!doc.getElementById('us_MiniClockStyle')) {
    fontSize = getValue('fontsize', 13);//dimensioni font

addStyle('#us_MiniClock{position:fixed!important;top:0px;right:0;cursor:move;width:65px;color:red;background-color:#181818;border:2px solid green;padding:1px 7px;font:' +
fontSize+'pt normal sans-serif;z-index:2147483647;}@media print{#us_MiniClock{display:none!important;}} \
').id = 'us_MiniClockStyle';
    if(div.offsetLeft) {
      window.onresize = OnReSize;
      div.addEventListener("mouseenter", OnMouseEnter, false);
      if(isFF)
        MenuCreate(obj('+MENU#us_MiniClockMenu,type=context'));
    } else {
      clearInterval(hTimer);
      div.parentNode.removeChild(div);
      spn = div = null;
    }
  }
}

function init() {
  try {
    if(!div) {
      create();
      if(!div)
        return;
    }
    var fse = FullScreenElement() || doc.body, wrp, st;
    if(fse.contains(div))
      return;
    switch(fse.tagName) {
      case 'VIDEO':
        if(getValue('allowAtVideo')) {
          st = !fse.paused;
          EndFullScreenMode();
          wrp = fse.parentNode;
          if(wrp.id != 'miniClockWrapper') {
            wrapper = wrp = obj('+DIV#miniClockWrapper');
            obj('+DIV#mcwrfsb,= ', wrp);
            wrp.firstChild.onclick = function(e) { if(!EndFullScreenMode()) SetFullScreenMode(wrapper); };
            wrp.appendChild(fse.parentNode.replaceChild(wrp, fse));
            wrp.addEventListener("mousemove", OnMouseMove);
          }
          if(isFullScreen())// why must it be asynchroniously?
            ForceEndFullscreen();
          SetFullScreenMode(wrp);
          InsertClock(wrp);
          if(st && fse.paused) {
            //console.log('video stopped while processed');
            fse.play();
          }
          window.setTimeout( function() {
            if(!isFullScreen())
              console.log('Clock could not reactivate full screen mode. Make sure, full-screen-api.allow-trusted-requests-only is false in about:config');
          }, 500);
        } else console.log();
      case 'IFRAME':
        break;
      default:
        InsertClock(fse);
    }

  } catch(e){ console.log(e); }
}

////////////
//Make the DIV element draggagle:
dragElement(document.getElementById("us_MiniClock"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}