Skip to content
This repository was archived by the owner on Jan 1, 2020. It is now read-only.

Commit 2244fd1

Browse files
committed
Safari support, yay!
1 parent b8b4e28 commit 2244fd1

File tree

7 files changed

+722
-0
lines changed

7 files changed

+722
-0
lines changed

safari.safariextension/Icon-32.png

1.23 KB
Loading

safari.safariextension/Icon-48.png

1.29 KB
Loading

safari.safariextension/Info.plist

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>Author</key>
6+
<string>Intars Students</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>keySharky</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>com.intarstudents.keysharky</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleShortVersionString</key>
14+
<string>1.5</string>
15+
<key>CFBundleVersion</key>
16+
<string>2</string>
17+
<key>Chrome</key>
18+
<dict>
19+
<key>Context Menu Items</key>
20+
<array>
21+
<dict>
22+
<key>Command</key>
23+
<string>keysharky-options</string>
24+
<key>Identifier</key>
25+
<string>keysharkyOptions</string>
26+
<key>Title</key>
27+
<string>keySharky Options</string>
28+
</dict>
29+
</array>
30+
<key>Database Quota</key>
31+
<real>1048576</real>
32+
<key>Global Page</key>
33+
<string>keysharky.html</string>
34+
</dict>
35+
<key>Content</key>
36+
<dict>
37+
<key>Scripts</key>
38+
<dict>
39+
<key>End</key>
40+
<array>
41+
<string>listener.js</string>
42+
</array>
43+
</dict>
44+
</dict>
45+
<key>Description</key>
46+
<string>Add missing keyboard functionality to Grooveshark!</string>
47+
<key>ExtensionInfoDictionaryVersion</key>
48+
<string>1.0</string>
49+
<key>Permissions</key>
50+
<dict>
51+
<key>Website Access</key>
52+
<dict>
53+
<key>Include Secure Pages</key>
54+
<true/>
55+
<key>Level</key>
56+
<string>All</string>
57+
</dict>
58+
</dict>
59+
<key>Update Manifest URL</key>
60+
<string>http://keysharky.tldr.lv/safari.plist</string>
61+
<key>Website</key>
62+
<string>http://keysharky.tldr.lv/</string>
63+
</dict>
64+
</plist>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<array/>
5+
</plist>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<html>
2+
<script>
3+
var keysharky = {
4+
5+
// Init keysharky object
6+
init: function(){
7+
keysharky.groovesharkTabID = null;
8+
keysharky.debug = true;
9+
10+
keysharky.defaults = {
11+
"play" : '{"modifiers":["control","alt","shift"],"keycode":90}',
12+
"stop" : '{"modifiers":["control","alt","shift"],"keycode":88}',
13+
"prev" : '{"modifiers":["control","alt","shift"],"keycode":65}',
14+
"next" : '{"modifiers":["control","alt","shift"],"keycode":68}',
15+
"favorite" : '{"modifiers":["control","alt"],"keycode":83}',
16+
"voteup" : '{"modifiers":["control","alt"],"keycode":65}',
17+
"votedown" : '{"modifiers":["control","alt"],"keycode":90}',
18+
"voteclear" : '{"modifiers":["control","alt"],"keycode":81}',
19+
};
20+
21+
safari.application.addEventListener("command", function(event) {
22+
if (event.command == "keysharky-options"){
23+
24+
var newTab = safari.application.activeBrowserWindow.openTab();
25+
newTab.url = safari.extension.baseURI + "options.html";
26+
27+
}
28+
}, false);
29+
30+
safari.application.addEventListener("message", function(request){
31+
32+
if (request.name == "keyup"){
33+
for(var toggle in keysharky.defaults){
34+
if (!localStorage[toggle]){
35+
localStorage[toggle] = keysharky.defaults[toggle];
36+
keysharky.log("localStorage fix: " + toggle + " reset");
37+
}
38+
}
39+
40+
// Fix for easier porting from Chrome
41+
request = request.message;
42+
43+
var t = null;
44+
var dismatch = null;
45+
var action = null;
46+
47+
// Is this combo valid?
48+
try{
49+
50+
for(var toggle in localStorage){
51+
dismatch = false;
52+
t = JSON.parse(localStorage[toggle]);
53+
54+
if (t["modifiers"].length != request["modifiers"].length)
55+
continue;
56+
57+
for(var i in t["modifiers"]){
58+
if (t["modifiers"][i] != request["modifiers"][i]){
59+
dismatch = true;
60+
break;
61+
}
62+
}
63+
64+
if (dismatch)
65+
continue;
66+
67+
if (t["keycode"] != request["keycode"])
68+
continue;
69+
70+
action = toggle;
71+
}
72+
73+
}catch(e){}
74+
75+
if (action){
76+
keysharky.log("Toggling '" + action + "' ...");
77+
78+
if (keysharky.groovesharkTabID != null){
79+
keysharky.toggle(action);
80+
}else{
81+
82+
keysharky.log("Searching for Grooveshark ...");
83+
keysharky.searchGrooveshark();
84+
85+
if (keysharky.groovesharkTabID != null){
86+
keysharky.toggle(action);
87+
}else{
88+
keysharky.log("Where is groove?!");
89+
}
90+
91+
}
92+
}
93+
94+
}
95+
},false);
96+
},
97+
98+
// Find Grooveshark tab
99+
searchGrooveshark: function(){
100+
101+
keysharky.groovesharkTabID = null;
102+
103+
for(var win in safari.application.browserWindows){
104+
if (keysharky.groovesharkTabID != null)
105+
break;
106+
107+
for(var tab in safari.application.browserWindows[win].tabs){
108+
if (keysharky.groovesharkTabID != null)
109+
break;
110+
111+
if (keysharky.checkUrl(safari.application.browserWindows[win].tabs[tab].url)){
112+
113+
keysharky.groovesharkTabID = {
114+
"win" : win,
115+
"tab" : tab
116+
};
117+
118+
keysharky.log("The groove is found!");
119+
120+
}
121+
}
122+
}
123+
},
124+
125+
// Check if Grooveshark haven't moved an inch
126+
checkUrl: function(url){
127+
return url.search(/^http\:\/\/(listen|preview|staging|retro)\.grooveshark\.com/) != -1 ? true : false;
128+
},
129+
130+
// Send toggle to Grooveshark tab
131+
toggle: function(action){
132+
var url = "";
133+
134+
try{
135+
url = safari.application.browserWindows[keysharky.groovesharkTabID["win"]].tabs[keysharky.groovesharkTabID["tab"]].url;
136+
}catch(e){}
137+
138+
if (!keysharky.checkUrl(url)){
139+
keysharky.searchGrooveshark();
140+
}
141+
142+
if (keysharky.groovesharkTabID != null){
143+
try{
144+
145+
safari.application.browserWindows[keysharky.groovesharkTabID["win"]].
146+
tabs[keysharky.groovesharkTabID["tab"]].page.dispatchMessage("Grooveshark", action);
147+
148+
keysharky.log("Toggled '" + request + "' !");
149+
150+
}catch(e){}
151+
}else{
152+
keysharky.log("Lost Grooveshark :(");
153+
}
154+
},
155+
156+
// Debugging is half of victory!
157+
log: function(msg){
158+
if (keysharky.debug && msg){
159+
console.log("keysharky ---> " + msg);
160+
}
161+
},
162+
163+
};
164+
165+
window.addEventListener("load", function(e) { keysharky.init(e); }, false);
166+
</script>
167+
</html>

safari.safariextension/listener.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var keysharkyListener = {
2+
3+
Grooveshark: function(toggle)
4+
{
5+
6+
var elem = document.createElement("script");
7+
elem.type = "text/javascript";
8+
elem.innerHTML = "if (typeof(Grooveshark) != 'undefined') Grooveshark." + toggle + ";";
9+
10+
var append = document.head.appendChild(elem);
11+
document.head.removeChild(append);
12+
},
13+
14+
init: function(){
15+
16+
if (window.location.href.search(/^http\:\/\/(listen|preview|staging|retro)\.grooveshark\.com/) != -1){
17+
safari.self.addEventListener("message", function(request){
18+
19+
var allToggles = {
20+
"play" : function(){ keysharkyListener.Grooveshark("togglePlayPause()"); },
21+
"stop" : function(){ keysharkyListener.Grooveshark("pause()"); },
22+
"prev" : function(){ keysharkyListener.Grooveshark("previous()"); },
23+
"next" : function(){ keysharkyListener.Grooveshark("next()"); },
24+
25+
"favorite" : function(){ keysharkyListener.Grooveshark("favoriteCurrentSong()"); },
26+
"voteup" : function(){ keysharkyListener.Grooveshark("voteCurrentSong(1)"); },
27+
"votedown" : function(){ keysharkyListener.Grooveshark("voteCurrentSong(-1)"); },
28+
"voteclear" : function(){ keysharkyListener.Grooveshark("voteCurrentSong(0)"); },
29+
};
30+
31+
if (request.name == "Grooveshark" && allToggles[request.message] != undefined){
32+
allToggles[request.message]();
33+
}
34+
35+
}, false);
36+
37+
}
38+
39+
console.log(window.location.href);
40+
if (window.location.href.search(/^safari\-extension\:\/\/com\.intarstudents\.keysharky/) == -1){
41+
this.unAllowedKeys = [16, 17, 18, 91];
42+
43+
// Inject in tab keyup listener, who will check for (maybe) valid keysharky combo
44+
window.addEventListener('keyup', function(event){
45+
46+
var modifiers = new Array();
47+
var key = '';
48+
var keycode = '';
49+
50+
// Get the modifiers
51+
if(event.metaKey) modifiers.push('meta');
52+
if(event.ctrlKey) modifiers.push('control');
53+
if(event.altKey) modifiers.push('alt');
54+
if(event.shiftKey) modifiers.push('shift');
55+
56+
// Get keycode
57+
if(event.keyCode) {
58+
keycode = event.keyCode;
59+
}
60+
61+
if(modifiers.length > 0 && !keysharkyListener.inArray(keysharkyListener.unAllowedKeys, keycode)) {
62+
63+
var request = {
64+
"modifiers" : modifiers,
65+
"keycode" : keycode,
66+
};
67+
68+
safari.self.tab.dispatchMessage("keyup", request);
69+
70+
}
71+
72+
}, false);
73+
}
74+
75+
},
76+
77+
// Check if Object is Array
78+
inArray: function(arr, value){
79+
var i;
80+
for (i=0; i < arr.length; i++) {
81+
if (arr[i] === value) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
};
88+
89+
try{
90+
91+
keysharkyListener.init();
92+
93+
}catch(e){
94+
// Fail, but with dignity!
95+
}

0 commit comments

Comments
 (0)