Skip to content

Commit

Permalink
VK fixes (now uses POST ajax requests)
Browse files Browse the repository at this point in the history
  • Loading branch information
romain.vallet committed Mar 10, 2011
1 parent 6067c0e commit 0f4aa03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
16 changes: 11 additions & 5 deletions background.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
var options;
var _gaq;

// Performs an ajax GET request
function ajaxGet(url, callback) {
// Performs an ajax request
function ajaxRequest(request, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
Expand All @@ -19,14 +19,20 @@
}
}
}
xhr.open('GET', url, true);
xhr.send();
xhr.open(request.method, request.url, true);
for (var i in request.headers) {
xhr.setRequestHeader(request.headers[i].header, request.headers[i].value);
}
xhr.send(request.data);
};

function onRequest(request, sender, callback) {
switch(request.action) {
case 'ajaxGet':
ajaxGet(request.url, callback);
ajaxRequest({url: request.url, method: 'GET'}, callback);
break;
case 'ajaxRequest':
ajaxRequest(request, callback);
break;
case 'showPageAction':
showPageAction(sender.tab);
Expand Down
47 changes: 29 additions & 18 deletions plugins/vk.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,36 @@ hoverZoomPlugins.push( {
prepareImgLinks: function(callback) {

function prepareFromPhotoId(link, photoId, listId) {
chrome.extension.sendRequest({action: 'ajaxGet', url: 'http://vk.com/al_photos.php?al=1&act=show&photo=' + photoId + '&list=' + listId}, function(data) {
var photos, url;
try {
photos = JSON.parse(data.match(/<!json>(.*)<!>/)[1]);
} catch(e) {
return;
}
for (var i in photos) {
if (photos[i].id == photoId) {
link.data('hoverZoomSrc', [photos[i].x_src]).addClass('hoverZoomLink');
} else {
// in case the request fetched details on another photo on the page
$('a[href^=/photo' + photos[i].id + ']').data('hoverZoomSrc', [photos[i].x_src]).addClass('hoverZoomLink');
if (!listId) {
listId = 'photos' + photoId.match(/(\d+)_/)[1];
}
chrome.extension.sendRequest({action: 'ajaxRequest',
url: 'http://vk.com/al_photos.php',
method: 'POST',
data: 'al=1&act=show&photo=' + photoId + '&list=' + listId,
headers: [{header: 'Content-Type', value: 'application/x-www-form-urlencoded'},
{header: 'X-Requested-With', value: 'XMLHttpRequest'}]},
function(data) {
var photos;
try {
//console.error('http://vk.com/al_photos.php?al=1&act=show&photo=' + photoId + '&list=' + listId);
//console.error(data);
photos = JSON.parse(data.match(/<!json>(.*)<!>/)[1]);
} catch(e) {
return;
}
}
if (!link.data('hoverZoomMouseLeft')) {
hoverZoom.displayPicFromElement(link);
}
});
for (var i in photos) {
if (photos[i].id == photoId) {
link.data('hoverZoomSrc', [photos[i].x_src]).addClass('hoverZoomLink');
} else {
// in case the request fetched details on another photo on the page
$('a[href^=/photo' + photos[i].id + ']').data('hoverZoomSrc', [photos[i].x_src]).addClass('hoverZoomLink');
}
}
if (!link.data('hoverZoomMouseLeft')) {
hoverZoom.displayPicFromElement(link);
}
});
}

$('a[href^=/photo]').mouseenter(function () {
Expand Down

0 comments on commit 0f4aa03

Please sign in to comment.