zheng.cuizhgmail.com / job

// Generated by CoffeeScript 1.10.0
(function() {
  var Job, KeyTrigger, Logger, Worker,
    slice = [].slice,
    bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  Job = (function() {
    Job.JOBS = [];

    Job.JOBSOBJ = {};

    function Job(title) {
      this.title = title;
      this.data = [];
    }

    Job.prototype.start = function() {
      console.log("Started");
      return console.log("Data size " + this.data.length + ", content: \n" + this.data);
    };

    Job.prototype.stop = function() {
      var blob;
      console.log("Stoped");
      if (this.data.length > 0) {
        console.log("download data " + this.data.length + " lines");
        blob = new Blob(this.data, {
          type: "text/plain;charset=utf-8"
        });
        saveAs(blob, "ttt-data.csv");
        console.log("clear data buffer.");
        return this.data = [];
      }
    };

    return Job;

  })();

  Worker = (function() {
    Worker.RUNNINGKEY = "running_key";

    function Worker(jobs) {
      var j, job, len;
      for (j = 0, len = jobs.length; j < len; j++) {
        job = jobs[j];
        Job.JOBS.push(job);
        Job.JOBSOBJ[job.id] = job;
      }
    }

    Worker.work = function() {
      var instance, jobs;
      jobs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
      instance = new Worker(jobs);
      return instance.work();
    };

    Worker.prototype.work = function() {
      this.kt = new KeyTrigger(this);
      return document.addEventListener("keydown", this.kt.keydown);
    };

    Worker.prototype.start = function() {
      var content, i, idx, j, job, ref;
      console.log("Working");
      if (!this.job) {
        content = "Press JOB Number to Start:\n";
        for (i = j = 0, ref = Job.JOBS.length - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
          job = Job.JOBS[i];
          content += i + ". " + job.title;
          content += "\n";
        }
        idx = prompt(content, "0");
        if (idx) {
          this.job = Job.JOBS[idx];
        }
      }
      if (this.job) {
        this.job.start();
        return GM_notification("任务执行完毕");
      } else {
        return GM_notification("未找到可执行的任务");
      }
    };

    Worker.prototype.stop = function() {
      if (this.job) {
        GM_notification("任务执行完毕,成功分析" + this.job.data.length + "条数据");
        this.job.stop();
        return this.job = null;
      } else {
        return GM_notification("无任务等待结束");
      }
    };

    return Worker;

  })();

  KeyTrigger = (function() {
    function KeyTrigger(worker) {
      this.worker = worker;
      this.keydown = bind(this.keydown, this);
      this.keys = [];
      this.status = "";
    }

    KeyTrigger.prototype.dispatch = function(rk) {
      this.keys.push(rk);
      if (this.check()) {
        if (this.status === "on") {
          this.keys = [];
          this.worker.start();
          return this.status = "";
        } else if (this.status === "off") {
          this.keys = [];
          return this.worker.stop();
        }
      } else {
        return console.log("not valid key");
      }
    };

    KeyTrigger.prototype.check = function() {
      var last_3_keys;
      last_3_keys = this.keys.slice(-3);
      if (this.keys.length >= 3 && (("" + last_3_keys) === ("" + ['T', 'T', 'T']))) {
        this.status = "on";
        return true;
      } else if (this.keys.length >= 3 && ("" + last_3_keys) === ("" + ['S', 'S', 'S'])) {
        this.status = "off";
        return true;
      } else {
        console.info("check False! TTT:" + last_3_keys + "   keys: " + this.keys);
        return false;
      }
    };

    KeyTrigger.prototype.keydown = function(e) {
      var keycode, realkey;
      keycode = e.which;
      realkey = String.fromCharCode(e.which);
      return this.dispatch(realkey);
    };

    return KeyTrigger;

  })();

  Logger = (function() {
    function Logger() {
      this.logContent = document.createElement('div');
      this.logContent.id = "logContent";
      this.logContent.style.cssText = "position:fixed;color:black;background-color:#fff;left:10px;bottom:10px;z-index:99999;font-size:24px;max-height:800px;_height:expression((document.documentElement.clientHeight||document.body.clientHeight)<800?'800px':''); overflow:hidden;";
      document.body.appendChild(this.logContent);
      this.logContent.setAttribute("readonly", "false");
      this.logLines = 0;
    }

    Logger.prototype.info = function(str) {
      var logEle, myDate, mytime;
      logEle = document.createElement('div');
      myDate = new Date();
      if (myDate.toLocaleTimeString) {
        mytime = myDate.toLocaleTimeString();
      } else {
        mytime = myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
      }
      this.logLines++;
      logEle.innerHTML = this.logLines + " " + mytime + " " + str;
      this.logContent.appendChild(logEle);
      while (this.logContent.childElementCount > 100) {
        this.logContent.innerHTML = "";
      }
      return this.logContent.scrollTop = this.logContent.scrollHeight;
    };

    return Logger;

  })();

}).call(this);