-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
91 lines (82 loc) · 2.87 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
85
86
87
88
89
90
91
/*
* Simulate click function (Youtube is nasty)
* Script found on http://stackoverflow.com/questions/6157929/how-to-simulate-a-mouse-click-using-javascript/6158050#6158050
*/
function simulate(element, eventName) {
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers){
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent){
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents'){
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
} else {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
} else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
/*
* This is where all the magic happends
* Event handling
*/
var remote = require('electron').remote;
var globalShortcut = remote.globalShortcut;
//Unregister events (Fixes error if html is refreshed)
globalShortcut.unregisterAll();
//Register keyboard events
globalShortcut.register('MediaPlayPause', function() {
console.log('MediaPlayPause');
simulate(document.getElementsByClassName("ytp-button ytp-play-button")[0], "click");
});
globalShortcut.register('MediaPreviousTrack', function() {
var prevItem = document.getElementsByClassName("ytp-button ytp-prev-button");
console.log('MediaPreviousTrack');
if(prevItem.length > 0) {
simulate(prevItem[0], "click");
} else {
alert('Nothing back...');
}
});
globalShortcut.register('MediaNextTrack', function() {
console.log('MediaNextTrack');
var nextItem = document.getElementsByClassName("ytp-button ytp-next-button");
if(nextItem.length > 0) {
simulate(nextItem[0], "click");
} else {
alert('Nothing next...');
}
});