-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (77 loc) · 2.19 KB
/
index.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
//Load needed SDK components
var { ToggleButton } = require('sdk/ui/button/toggle');
var contextMenu = require("sdk/context-menu");
var panels = require("sdk/panel");
var self = require("sdk/self");
var notifications = require("sdk/notifications");
//Setup toggle to attach panel to
var button = ToggleButton({
id: "my-button",
label: "YouTube Player",
icon: {
"16": "./YouTube.gif"
},
onChange: handleChange
});
//Create the panel in it's initial state
var panel = panels.Panel({
width: 512,
height: 320,
contentURL: 'http://www.youtube.com/',
contentScriptFile: './player.js',
onHide: handleHide
});
//Setup context menu item to get videos
var menuItem = contextMenu.Item({
label: "Add Video",
context: contextMenu.SelectorContext("a[href*='/watch?']"),
contentScript: 'self.on("click", function (node, data) {' +
' self.postMessage(node.href);' +
'});',
onMessage: function (imgSrc) {
console.log(imgSrc);
reg = /youtube.com/;
if (reg.test(imgSrc)) {
var videoId = imgSrc.split('=')[1];
console.log(videoId);
panel.port.emit('cue-video', videoId);
}
}
});
//Setup context menu for player controls
var menu = contextMenu.Menu({
label: "Player Controls",
contentScript: 'self.on("click", function (node, data) {' +
' console.log("You clicked " + data);' +
' self.postMessage(data);' +
'});',
items: [
contextMenu.Item({ label: "Play", data: "play" }),
contextMenu.Item({ label: "Pause", data: "pause" }),
contextMenu.Item({ label: "Next Video", data: "next" }),
contextMenu.Item({ label: "Previous Video", data: "previous" }),
contextMenu.Item({ label: "Restart Playlist", data: "restart" }),
contextMenu.Item({ label: "Clear", data: "clear" }),
],
onMessage: function (command) {
panel.port.emit(command + '-video');
}
});
//Panel functions to control show and hide
function handleChange(state) {
if (state.checked) {
panel.show({
position: button
});
}
}
function handleHide() {
button.state('window', {checked: false});
}
//Port listeners
panel.port.on('playlist-alert', function(text) {
notifications.notify({
title: 'Youtube Player',
text: text
});
});