Permalink
Browse files

Rewritten VK search for non logged users

  • Loading branch information...
1 parent d9dc803 commit 16541442a95c7b18739960097d1d52fc69ebd745 @buger committed Dec 29, 2011
View
@@ -15,6 +15,18 @@
"popup": "index.html"
},
+ "content_scripts": [
+ {
+ "css": [ "assets/css/mp3_player_button.css", "assets/css/style.css" ],
+ "js": [ "plugins/lastfm/content_script.js" ],
+ "matches": [ "http://*.last.fm/*", "http://*.lastfm.ru/*","http://*.lastfm.pt/*", "http://*.lastfm.de/*", "http://*.lastfm.es/*", "http://*.lastfm.fr/*", "http://*.lastfm.it/*", "http://*.lastfm.jp/*", "http://*.lastfm.pl/*", "http://*.lastfm.com.br/*", "http://*.lastfm.se/*", "http://*.lastfm.com.tr/*"]
+ },
+ {
+ "js": ["plugins/vkontakte/oauth_callback.js"],
+ "matches": ["http://chromusapp-v2.appspot.com/chromus/plugins/vkontakte/oauth.html*"]
+ }
+ ],
+
"background_page": "background.html",
"permissions": ["notifications", "tabs", "http://*/", "https://*/"]
No changes.
@@ -0,0 +1,2 @@
+
+
@@ -4,28 +4,39 @@
</head>
<body>
- <script type="text/javascript">
- if (document.location.hash.match(/access_token/)) {
- var hash = document.location.hash.substr(1).split('&');
- var auth = {}
+ <h1 id="logged" style="color: green; display: none">Logged successfully!</h1>
+ <p id="logged2" style="display: none">Now you can close window and open player</h1>
- for (var param, i=0, l=hash.length; i<l; i++) {
- param = hash[i].split('=');
+ <script type="text/javascript">
+ if (window.opener) {
+ if (document.location.hash.match(/access_token/)) {
+ var hash = document.location.hash.substr(1).split('&');
+ var auth = {}
- auth[param[0]] = param[1];
- }
-
- if (window.pokki)
- auth['session_id'] = pokki.scramble(auth['session_id'])
-
- window.opener.setVKSession(auth);
- } else if (document.location.hash.match(/error/)) {
- window.parent.setVKSession({ error: true });
- }
+ for (var param, i=0, l=hash.length; i<l; i++) {
+ param = hash[i].split('=');
- setTimeout(function(){
- window.close();
- });
+ auth[param[0]] = param[1];
+ }
+
+ if (window.pokki)
+ auth['session_id'] = pokki.scramble(auth['session_id'])
+
+ window.opener.setVKSession(auth);
+ } else if (document.location.hash.match(/error/)) {
+ window.parent.setVKSession({ error: true });
+ }
+
+ setTimeout(function(){
+ window.close();
+ });
+ } else {
+ document.getElementById('logged').style.display = 'block';
+
+ setTimeout(function(){
+ window.close();
+ }, 2000);
+ }
</script>
</body>
</html>
@@ -0,0 +1,23 @@
+console.warn('asdasd', document.location, document.location.hash, document.location.hash.match(/access_token/))
+
+if (document.location.hash.match(/access_token/)) {
+ var hash = document.location.hash.substr(1).split('&');
+ var auth = {}
+
+ for (var param, i=0, l=hash.length; i<l; i++) {
+ param = hash[i].split('=');
+
+ auth[param[0]] = param[1];
+ }
+
+ message = {
+ method: 'vk:auth',
+ auth: auth
+ }
+
+ console.warn('sending auth', auth)
+
+ chrome.extension.sendRequest(message);
+} else if (document.location.hash.match(/error/)) {
+ chrome.extension.sendRequest({ method:'vk:auth', auth: {error: true}});
+}
@@ -4,12 +4,19 @@ VK =
APP_ID: if global.debug then "2649785" else "2698877"
SCOPE: "audio,offline"
-
authURL: ->
+ if browser.isChrome
+ baseLocation = "#{chromus.baseURL}/chromus/index.html"
+ else
+ baseLocation = document.location.toString()
+
+ plugin_path = chromus.plugins_info.vkontakte.path
+ plugin_path = plugin_path.replace("chrome-extension://oeghnnflghjikgempjanmfekicakholb/","")
+
redirect_uri = [
- document.location.toString()
+ baseLocation
"/../"
- chromus.plugins_info.vkontakte.path
+ plugin_path
"/oauth.html"
].join('')
@@ -23,54 +30,15 @@ VK =
].join('&')
- searchWithoutLogin: (args, callback) ->
- BaseURL = "http://api.vk.com/api.php"
- SignURL = "http://chromusapp.appspot.com/sign_data"
-
- query = "#{args.artist} #{args.song}"
-
- callback_name = "vkclb#{chromus.utils.uid()}"
-
+ searchWithoutLogin: (args, callback) ->
$.ajax
- url: "#{SignURL}"
- data:
- track: query
- dataType: "jsonp"
- cache: true
- jsonpCallback: callback_name
-
- success: (resp) =>
- data =
- 'api_id': resp.api_key
- 'method': 'audio.search'
- 'format': 'json'
- 'sig': resp.signed_data
- 'sort': 2 # popular first
- 'test_mode': 1
- 'count': 10
- 'q': query
-
- $.ajax
- url: "#{BaseURL}"
- data: data,
- dataType: "jsonp"
- cache: true
- jsonpCallback: callback_name
-
- success: (result) ->
- return callback [] unless result.response
-
- records = _.map _.rest(result.response), (i) ->
- {
- artist: i.audio.artist
- song: i.audio.title
- duration: parseInt(i.audio.duration)
- file_url: i.audio.url
- source_title: "Vkontakte"
- source_icon: "http://vkontakte.ru/favicon.ico"
- }
-
- callback(records)
+ url: "#{chromus.baseURL}/api/token/get"
+ data: data
+ dataType:"jsonp"
+ success: (resp) ->
+ unless resp.error
+ args.access_token = resp.token
+ VK.searchAPI(args, callback)
searchAPI: (args, callback) ->
@@ -84,10 +52,13 @@ VK =
sort: 2
count: 10
- if browser.isPokki
- data.access_token = pokki.descrumble store.get('vk:token')
+ unless args.access_token
+ if browser.isPokki
+ data.access_token = pokki.descrumble store.get('vk:token')
+ else
+ data.access_token = store.get('vk:token')
else
- data.access_token = store.get('vk:token')
+ data.access_token = args.access_token
$.ajax
url: "https://api.vkontakte.ru/method/audio.search"
@@ -120,4 +91,22 @@ VK =
@chromus.registerPlugin("vkontakte", VK)
-@chromus.registerAudioSource("vkontakte", VK)
+@chromus.registerAudioSource("vkontakte", VK)
+
+
+# Listen for messages
+browser.addMessageListener (msg, sender, sendResponse) ->
+ switch msg.method
+ when "vk:auth"
+ store.set "vk:token", msg.auth.access_token
+ store.set "vk:user_id", msg.auth.user_id
+
+ $.ajax
+ url: "#{chromus.baseURL}/api/token/add"
+ data:
+ token: msg.auth.access_token
+ dataType:"jsonp"
+ success: (resp) ->
+ console.log 'token added'
+
+ console.warn "logged!"
@@ -7,59 +7,27 @@
APP_ID: global.debug ? "2649785" : "2698877",
SCOPE: "audio,offline",
authURL: function() {
- var link, redirect_uri;
- redirect_uri = [document.location.toString(), "/../", chromus.plugins_info.vkontakte.path, "/oauth.html"].join('');
+ var baseLocation, link, plugin_path, redirect_uri;
+ if (browser.isChrome) {
+ baseLocation = "" + chromus.baseURL + "/chromus/index.html";
+ } else {
+ baseLocation = document.location.toString();
+ }
+ plugin_path = chromus.plugins_info.vkontakte.path;
+ plugin_path = plugin_path.replace("chrome-extension://oeghnnflghjikgempjanmfekicakholb/", "");
+ redirect_uri = [baseLocation, "/../", plugin_path, "/oauth.html"].join('');
return link = ["http://api.vkontakte.ru/oauth/authorize?", "client_id=" + VK.APP_ID, "scope=" + VK.SCOPE, "redirect_uri=" + redirect_uri, "display=popup", "response_type=token"].join('&');
},
searchWithoutLogin: function(args, callback) {
- var BaseURL, SignURL, callback_name, query;
- var _this = this;
- BaseURL = "http://api.vk.com/api.php";
- SignURL = "http://chromusapp.appspot.com/sign_data";
- query = "" + args.artist + " " + args.song;
- callback_name = "vkclb" + (chromus.utils.uid());
return $.ajax({
- url: "" + SignURL,
- data: {
- track: query
- },
+ url: "" + chromus.baseURL + "/api/token/get",
+ data: data,
dataType: "jsonp",
- cache: true,
- jsonpCallback: callback_name,
success: function(resp) {
- var data;
- data = {
- 'api_id': resp.api_key,
- 'method': 'audio.search',
- 'format': 'json',
- 'sig': resp.signed_data,
- 'sort': 2,
- 'test_mode': 1,
- 'count': 10,
- 'q': query
- };
- return $.ajax({
- url: "" + BaseURL,
- data: data,
- dataType: "jsonp",
- cache: true,
- jsonpCallback: callback_name,
- success: function(result) {
- var records;
- if (!result.response) return callback([]);
- records = _.map(_.rest(result.response), function(i) {
- return {
- artist: i.audio.artist,
- song: i.audio.title,
- duration: parseInt(i.audio.duration),
- file_url: i.audio.url,
- source_title: "Vkontakte",
- source_icon: "http://vkontakte.ru/favicon.ico"
- };
- });
- return callback(records);
- }
- });
+ if (!resp.error) {
+ args.access_token = resp.token;
+ return VK.searchAPI(args, callback);
+ }
}
});
},
@@ -73,10 +41,14 @@
sort: 2,
count: 10
};
- if (browser.isPokki) {
- data.access_token = pokki.descrumble(store.get('vk:token'));
+ if (!args.access_token) {
+ if (browser.isPokki) {
+ data.access_token = pokki.descrumble(store.get('vk:token'));
+ } else {
+ data.access_token = store.get('vk:token');
+ }
} else {
- data.access_token = store.get('vk:token');
+ data.access_token = args.access_token;
}
return $.ajax({
url: "https://api.vkontakte.ru/method/audio.search",
@@ -113,4 +85,23 @@
this.chromus.registerAudioSource("vkontakte", VK);
+ browser.addMessageListener(function(msg, sender, sendResponse) {
+ switch (msg.method) {
+ case "vk:auth":
+ store.set("vk:token", msg.auth.access_token);
+ store.set("vk:user_id", msg.auth.user_id);
+ $.ajax({
+ url: "" + chromus.baseURL + "/api/token/add",
+ data: {
+ token: msg.auth.access_token
+ },
+ dataType: "jsonp",
+ success: function(resp) {
+ return console.log('token added');
+ }
+ });
+ return console.warn("logged!");
+ }
+ });
+
}).call(this);
@@ -8,8 +8,8 @@ template = Handlebars.compile '
{{#if logged}}
<ul>
<li>
- <b>Logged!</b>
- <p>Rest of functionality will be available in next update</p>
+ <h3 style="font-size:20px; font-weight: bold; margin-bottom: 10px;">Logged!</h3>
+ <p style="font-size: 14px;">Rest of functionality will be available in next update. Stay tuned!</p>
</li>
<li>
<a class="btn logout" style="color: red">Logout</a>
@@ -66,7 +66,15 @@ class UI extends Backbone.View
"Vkontakte",
"status=0,toolbar=0,location=0,menubar=0,resizable=1"
- logout: ->
+ logout: ->
+ $.ajax
+ url: "#{chromus.baseURL}/api/token/delete"
+ data:
+ token: store.get "vk:token"
+ dataType:"jsonp"
+ success: (resp) ->
+ console.log 'token removed'
+
store.remove "vk:token"
store.remove "vk:user_id"
Oops, something went wrong.

0 comments on commit 1654144

Please sign in to comment.