forked from emoneyx5/PinPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.js
More file actions
150 lines (131 loc) · 4.37 KB
/
widget.js
File metadata and controls
150 lines (131 loc) · 4.37 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// // Global Namespace
var PinPoint = PinPoint || {};
// Widget Class called in main
PinPoint.Widget = function(video){
this.video = video;
this.videoParent = document.querySelector("video").parentNode;
this.videoParent.addEventListener('mouseenter', function(event){
this.drawSideBar();
}.bind(this));
this.videoParent.addEventListener('mouseleave', function(event){
if (event.fromElement === this.videoParent && event.toElement != this.sideBar) {
this.destroySideBar();
}
}.bind(this));
};
PinPoint.Widget.prototype = {
// stops clicks from being registered through widget
onSideBarClick: function(event){
event.stopPropagation();
},
// draws sidebar if enabled is true
drawSideBar: function(){
chrome.runtime.sendMessage({ url: this.getUrl() }, function(response){
if (response.enable) {
this.sideBar = document.createElement("div");
this.sideBar.setAttribute("class", "pinpoint-sideBar");
this.sideBar.addEventListener('click', this.onSideBarClick.bind(this));
this.sideBar.style.display = "block";
this.sideBar.style.position = "absolute";
this.sideBar.style.top = this.videoParent.offsetTop + "px";
this.sideBar.style.left = this.videoParent.offsetLeft + "px";
this.sideBar.style.backgroundColor = "rgb(37,37,37)";
this.sideBar.style.zIndex = 5e6;
this.video.offsetParent.appendChild(this.sideBar);
this.drawForm();
this.drawTable();
this.appendNotes();
}
}.bind(this));
},
destroySideBar: function(){
if (this.sideBar) {
this.sideBar.parentNode.removeChild(this.sideBar);
this.sideBar = null;
}
},
// draws input fields
drawForm: function(){
this.form = document.createElement("form");
this.form.setAttribute('class',"pinpoint-add-note");
this.form.addEventListener('submit', this.createNote.bind(this));
this.input = document.createElement("input");
this.input.setAttribute('type', 'text');
this.input.setAttribute('placeholder', 'Create a PinPoint here...');
this.input.setAttribute('class', 'pinpoint-note-input');
// Stops youtube keyboard shortcuts from interfering when typing a comment.
this.input.addEventListener('keypress', function(event){
event.stopPropagation();
});
this.submit = document.createElement("input");
this.submit.setAttribute('type',"submit");
this.submit.setAttribute('class',"pinpoint-save");
this.submit.setAttribute('value',"Save note");
this.form.appendChild(this.input);
this.form.appendChild(this.submit);
this.sideBar.appendChild(this.form);
},
// draws divs for css
drawTable: function() {
this.tableContainer = document.createElement("div");
this.tableContainer.setAttribute('class', "pinpoint-notes-container");
this.sideBar.appendChild(this.tableContainer);
},
// creates a note object literal and fires the add
// note message to popup.js
createNote: function(event){
event.preventDefault();
var noteContentFromForm = this.input.value;
var time = document.getElementsByClassName('ytp-time-current')[0].innerHTML;
var note = {
title: document.title,
noteTime: time,
content: noteContentFromForm,
seconds: this.video.currentTime,
url: this.getUrl()
};
chrome.runtime.sendMessage({
method: "add note",
url: this.getUrl(),
note: note
}, this.appendNotes.bind(this));
this.input.value = "";
},
// asks JSONparser for the notes array by giving it
// the current URL.
appendNotes: function(){
chrome.runtime.sendMessage({ url: this.getUrl() }, function(response){
var notes = response.notesArray;
this.sortNotes(notes);
this.tableContainer.innerHTML = "";
for (note of notes) {
var node = new PinPoint.NotePresenter(
note,
this.getUrl(),
this.appendNotes.bind(this)).present();
this.tableContainer.appendChild(node);
}
}.bind(this));
},
// sorts the notes based on seconds
sortNotes: function(notesArray){
notesArray.sort(function(a,b) {
return a.seconds - b.seconds;
});
},
// returns the url of the current tab.
getUrl: function(){
return this.video.baseURI;
},
};
// main loop attaches a widget instance to all videos on
// the page.
function main(){
var videos = document.querySelectorAll("video");
for (var i = 0; i < videos.length; i++){
videos[i].pinPointWidget = videos[i].pinPointWidget || new PinPoint.Widget(videos[i]);
}
}
window.addEventListener('DOMNodeInserted', function(){
main();
});