-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.js
More file actions
130 lines (92 loc) · 3.82 KB
/
global.js
File metadata and controls
130 lines (92 loc) · 3.82 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
var youtubeVidID = ""
var clearJSON = "%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Playlist.Clear%22%2C%22params%22%3A%7B%22playlistid%22%3A1%7D%2C%22id%22%3A1%7D";
var playJSON = "%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Player.Open%22%2C%22params%22%3A%7B%22item%22%3A%7B%22playlistid%22%3A1%7D%7D%2C%22id%22%3A1%7D%0A";
var add1JSON = "%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Playlist.Add%22%2C%22params%22%3A%7B%22playlistid%22%3A1%2C%22item%22%3A%7B%22file%22%3A%22plugin%3A%2F%2Fplugin.video.youtube%2F%3Faction%3Dplay_video%26videoid%3D";
var add2JSON = "%22%7D%7D%2C%22id%22%3A1%7D";
var open1JSON = "%7B%22jsonrpc%22%3A%20%222.0%22%2C%20%22method%22%3A%20%22Player.Open%22%2C%20%22params%22%3A%7B%22item%22%3A%20%7B%22file%22%20%3A%20%22plugin%3A%2F%2Fplugin.video.youtube%2F%3Faction%3Dplay_video%26videoid%3D";
var open2JSON = "%22%20%7D%7D%2C%20%22id%22%20%3A%20%221%22%7D%0A";
var test = "%7B%22jsonrpc%22%3A%20%222.0%22%2C%20%22method%22%3A%20%22Player.Open%22%2C%20%22params%22%3A%7B%22item%22%3A%20%7B%22file%22%20%3A%20%22plugin%3A%2F%2Fplugin.video.youtube%2F%3Faction%3Dplay_video%26videoid%3DhH8EYvnlDxQ%22%20%7D%7D%2C%20%22id%22%20%3A%20%221%22%7D%0A"
var serverHeader = "http://192.168.178.22/jsonrpc?request=";
var xbmcip = "";
var xbmcport = "";
function JSONresponse() {
//Response from the XMLHttpRequest for the console
console.log(this.responseText);
}
var sendData = function(event) {
// Button press
if(event.command !== "xbmc s") return;
var url = event.target.browserWindow.activeTab.url;
console.log(url);
if(parseVideoID(url))
return;
//send to XBMC
//sendJSON("clear");
//sendJSON("add");
//sendJSON("play");
//sendJSON("test");
sendJSON("clear");
sendJSON("open");
//File start.js
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("stop Player", null);
};
var parseVideoID = function(urlstring) {
var urlYT = urlstring.search(/youtube.+/);
if (urlYT == -1)
return 1;
var urlSub = urlstring.substring(32,urlstring.length);
urlYT = urlSub.search("=");
if(urlYT >= 3)
urlSub = urlSub.substring(0,urlYT); //if there is a = after the Video id
urlYT = urlSub.search("&");
if(urlYT >= 3)
urlSub = urlSub.substring(0,urlYT); //if there is a & after the Video id
console.log(urlSub);
youtubeVidID = urlSub;
return 0;
};
var sendJSON = function(com) {
var myRequest = new XMLHttpRequest();
//var endpoint = serverHeader;
getSettings();
console.log('XBMC IP: ' + xbmcip);
console.log('XBMC Port: ' + xbmcport);
var endpoint = "http://" + xbmcip + ":" + xbmcport + "/jsonrpc?request=";
console.log('com: ' + com);
if(com === "clear")
endpoint += clearJSON;
else if(com === "add")
endpoint += add1JSON + youtubeVidID + add2JSON;
else if(com === "play")
endpoint += playJSON;
else if(com === "test")
endpoint += test;
else if(com === "open")
endpoint += open1JSON + youtubeVidID + open2JSON;
myRequest.open("GET", endpoint);
console.log('opening ' + endpoint);
myRequest.onload = JSONresponse;
myRequest.send();
};
var getSettings = function(){
xbmcip = safari.extension.settings.getItem("xbmcip");
xbmcport = safari.extension.settings.getItem("xbmcport");
};
var validate_shorturl = function(event) {
if(event.command === "shorten url") {
// Disable the button if there is no URL loaded in the tab.
event.target.disabled = !event.target.browserWindow.activeTab.url;
}
};
var handle_message = function(msgEvent) {
var messageName = msgEvent.name;
var messageData = msgEvent.message;
console.log(messageName);
console.log(messageData);
if(messageName === 'test') {
//handle_shorturl(messageData);
}
};
safari.application.addEventListener("command", sendData, false);
safari.application.addEventListener("validate", validate_shorturl, false);
safari.application.addEventListener("message", handle_message, false);