youpy (owner)

Revisions

gist: 138781 Download_button fork
public
Public Clone URL: git://gist.github.com/138781.git
Embed All Files: show embed
teiten_uploader.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ==UserScript==
// @name Teiten Uploader
// @namespace http://d.hatena.ne.jp/youpy/
// @include http://teiten.org/everyone*
// ==/UserScript==
 
(function() {
  GM_addStyle('.fav-add { display: none; }');
 
  Number.prototype.times = function(f) {
    var times = Math.abs(this.valueOf());
 
    for(var i = 0; i < times; i ++) {
      f(i);
    }
  };
 
  function formatHex(hex) {
    return hex.length == 1 ? '0' + hex : hex;
  }
 
  function getColors() {
    var image = document.images[Math.floor(Math.random() * document.images.length)];
 
    canvas.width = image.naturalWidth;
    canvas.height = image.naturalHeight;
    ctx.drawImage(image, 0, 0);
 
    var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
    var Effects = {};
 
    Effects.thru = function(v) {
      return v;
    };
 
    var effect = Effects.thru;
    var colors = [];
    var line, lines;
 
    lines = [];
 
    (canvas.width).times(function(w) {
      line = [];
 
      (canvas.height).times(function(h) {
        var pos = (canvas.width * h + w) * 4;
 
        var r = formatHex(effect(imageData[pos]).toString(16));
        var g = formatHex(effect(imageData[pos+1]).toString(16));
        var b = formatHex(effect(imageData[pos+2]).toString(16));
 
        line.push(r + g + b);
      });
 
      // workaround for misalignment
      line.push('000000');
 
      lines.push(line);
    });
 
    var sort_key = Math.floor(Math.random() * canvas.height);
 
    colors = lines.sort(function(a, b) {
      return parseInt(b[sort_key], 16) - parseInt(a[sort_key], 16);
    }).reduce(function(line, result) {
      return result.concat(line);
    }, []);
 
    return colors.join('%2C') + '&h=' + canvas.height + '&w=' + canvas.width;
  }
 
  function upload() {
    GM_xmlhttpRequest({
      method: 'post',
      url: 'http://teiten.org/snapshot.php',
      data: 'colors=' + getColors(),
      headers: {
          'Content-type': 'application/x-www-form-urlencoded'
      },
      onload: function(res) {
        location.reload();
      }
    });
  }
 
  var canvas = unsafeWindow.document.createElement('canvas');
  canvas.style.display = 'inline';
  unsafeWindow.document.body.appendChild(canvas);
  var ctx = canvas.getContext("2d");
 
  setInterval(upload, 1000 * 60 * 1);
})();