randajan / iTcan Webkit

// ==UserScript==
// @name         iTcan Webkit
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Custom web browser extensions for itcan company
// @author       randajan
// @match        *://*/*
// @icon         https://itcan.cz/static/img/favicon/apple-icon-57x57.png
// @grant        none
// @copyright    2021, randajan (https://openuserjs.org/users/randajan)
// @updateURL    https://openuserjs.org/meta/randajan/iTcan_Webkit.meta.js
// @downloadURL  https://openuserjs.org/install/randajan/iTcan_Webkit.user.js
// @license      MIT
// ==/UserScript==

(function() {
    var W = window, D = document;

    var searchSelection = {
        "Mouse1 + KeyP":function(sl) { W.open("https://www.cpubenchmark.net/cpu.php?cpu="+sl); },
        "Mouse1 + KeyO":function(sl) { W.open("https://www.google.com/search?q=passmark+"+sl); },
        "Mouse1 + KeyU":function(sl) {
            if (!window.scrapeByItcan) { return; }
            var text = sl.toString();
            window.scrapeByItcan("passmark/cpu", {cpuname:text})
                .then(function(r) {return r.json();})
                .then(function(r) {
                if (r.error) { window.alert(r.error.msg); }
                var score = r.result.score;
                var scoreText = "<b>"+score.total+"</b>/"+score.single;
                var href = r.url;
                var title = r.result.name + "\n" + r.result.socket;
                var style = "border:1px solid black; background-color:#FFFF00; color:black; margin-left:3px; border-radius:3px; padding: 0 3px;";
                var el = "<a style='"+style+"' href='"+href+"' target='_blank' title='"+title+"'>"+scoreText+"</a>";
                document.body.innerHTML = document.body.innerHTML.replaceAll(text, text+el);
            });
        }
    };

    var ControlDetector = new (function() {
        var timer = {};
        var reg = {};
        var key;
        var debug = false;

        function init() { key = {press:{}, combo:[]}; }

        function serialize(a) { return a.join(" + "); }

        function press(k) {
          if (key.press[k] === true) { return false; }
          key.press[k] = true;
          key.combo.push(k);
          var combo = serialize(key.combo);
          if (debug) { console.log("press", key.combo); }
          var f = reg[combo];
          if (f) { f(); }
          return true;
        }

        function release(k) {
          if (key.press[k] !== true) { return false; }
          key.combo.splice(key.combo.indexOf(k), 1);
          delete key.press[k];
          if (debug) { console.log("release", key.combo); }
          return true;
        }

        function pressing(k) {
          var change = press(k);
          clearTimeout(timer[k]);
          timer[k] = setTimeout(function() { release(k); }, change ? 5000 : 100);
        }

        D.addEventListener("keydown", function(ev) {pressing(ev.code);});
        D.addEventListener("keyup", function(ev) {release(ev.code);});

        D.addEventListener("mousedown", function(ev) {press("Mouse"+ev.which);});
        D.addEventListener("mouseup", function(ev) {release("Mouse"+ev.which);});

        W.addEventListener('blur', init);

        this.add = function(keys, callback) {
            var combo = keys instanceof Array ? serialize(keys) : String(keys);
            if (reg[combo]) { throw "KeyDetector.add() detect duplicate keys"; }
            reg[combo] = callback;
        }

        init();

        this.add(['ControlLeft', 'AltRight', 'KeyC', 'KeyD'], function() { debug = !debug; console.log("ControlDetector debug", debug ? "start" : "end"); });

    })();

    function Selection() {
        var sys = window.getSelection ? "win" : (document.selection && document.selection.type != "Control") ? "doc" : null;
        var range = sys === "win" ? window.getSelection().getRangeAt(0) : sys === "doc" ? document.selection.createRange() : null;

        this.appendHtml = function(html, isBefore) {
          if (!sys) { return; }
          range.collapse(isBefore);
          if (sys === "win") {
                var el = document.createElement("div");
                el.innerHTML = html;
                var node, frag = document.createDocumentFragment();
                while ( node = el.firstChild ) { frag.appendChild(node); }
                range.insertNode(frag);
          } else {
                range.pasteHTML(html);
          }
        }

        this.blur = function() {
            if (sys === "win") {window.getSelection().removeAllRanges();}
            else if (sys === "doc") {document.selection.empty();}
        }

        this.toString = function() {
          return (sys === "win" ? range.toString() : sys === "doc" ? String(range.text) : "").trim();
        }

        this.isBlank = function() { return !sys || !this.toString(); }

    }

    function searchSelectionFactory(combo, callback) {
        return function() { var sl = new Selection(); if (!sl.isBlank()) {callback(sl);} }
    }

    for (var combo in searchSelection) {
        ControlDetector.add(combo, searchSelectionFactory(combo, searchSelection[combo]));
    }

})();