-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab-workbook-userscript.js
103 lines (91 loc) · 3.49 KB
/
lab-workbook-userscript.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
94
95
96
97
98
99
100
101
102
103
// ==UserScript==
// @name Lab Workbook for Trello
// @namespace http://tampermonkey.net/
// @version 0.1
// @description dump results into S3, stream progress to Trello
// @author You
// @match https://trello.com/*
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// ==/UserScript==
/* jshint -W097 */
'use strict';
function inheritsFrom(child, parent) {
child.prototype = Object.create(parent.prototype);
}
function workbookInject(){
// Step 1: Add injection handler
unsafeWindow.TFM.description.parseInlineOutput.link = maybeInsertLink(
unsafeWindow.TFM.description.parseInlineOutput.link);
// Step 2: Add Dygraph library for interactive graphing :)
var dygraph = document.createElement('script');
dygraph.setAttribute("src", "//cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js");
document.head.appendChild(dygraph);
}
function maybeInsertLink(oldLinkHandler) {
return function(link){
if (/WORKBOOK_IMAGE/.exec(link.url)) {
console.log("Creating image...");
var element = new WorkbookAutomaticImage(link.url);
return this.html.apply(this, element.createNewElement());
} else if (/WORKBOOK_PLOT/.exec(link.url)) {
console.log("Creating plot...");
var element = new WorkbookAutomaticPlot(link.url);
return this.html.apply(this, element.createNewElement());
} else {
return oldLinkHandler.call(this, link);
}
};
}
function WorkbookAutomaticElement(url) {
this.url = url;
}
WorkbookAutomaticElement.prototype.createNewElement = function(){
// Call me when rendering
// Does several things:
// - Returns the HTML element placeholder for you to fill :)
// - Registers a callback to populate `this.element` when it has
// been rendered by Trello.
var self = this;
this.document_id = "workbook-";
for (var i=0; i<10; i++) {
this.document_id += "0123456789abcdef"[Math.floor(Math.random()*16)];
}
unsafeWindow.requestAnimationFrame(function(){
// Assign javascript to this element
self.element = document.getElementById(self.document_id);
self.onBound();
});
return this.html();
};
WorkbookAutomaticElement.prototype.html = function(){
return ["div", "", {attrs: {id: this.document_id}}];
};
WorkbookAutomaticElement.prototype.onBound = function(){};
///////////// Image support /////////////
function WorkbookAutomaticImage(){
WorkbookAutomaticElement.apply(this, arguments);
}
inheritsFrom(WorkbookAutomaticImage, WorkbookAutomaticElement);
WorkbookAutomaticImage.prototype.onBound = function(){
$("<img>").attr({src: this.url}).appendTo(this.element);
};
///////////// Plot support /////////////
function WorkbookAutomaticPlot(){
WorkbookAutomaticElement.apply(this, arguments);
}
inheritsFrom(WorkbookAutomaticPlot, WorkbookAutomaticElement);
WorkbookAutomaticPlot.prototype.onBound = function(){
var self = this;
GM_xmlhttpRequest({method: "GET",
url: this.url,
ignoreCache: true,
onload: function(xhr){
$(self.element).on('click', function(e){console.log("CLICKY");return e.stopPropagation();});
var options = $.parseJSON(xhr.responseText);
console.log(options);
self.graph = new Dygraph(self.element, options.file, options);
}
});
};
workbookInject();