-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgapi_wishlist.js
More file actions
158 lines (143 loc) · 5.26 KB
/
sgapi_wishlist.js
File metadata and controls
158 lines (143 loc) · 5.26 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* SteamGifts Userscript API
* Wishlist Tools
*
*/
SgApi.Plugins.register("Wishlist Tools", "0.1.1", function(){
/**
*
* Namespace for the Wishlist Tools.
*
* @name Wishlist
* @static
* @memberof SgApi
* @declared in sgapi_wishlist.js
*/
SgApi.Wishlist = new function(){
/**
*
* Wishlist container
* @memberof SgApi.Wishlist
* @declared in sgapi_wishlist.js
*/
var WishlistContainer = function(list){
var self = this;
/**
*
* Check if a game is on your wishlist.
*
* @memberof SgApi.Wishlist.WishlistContainer
*
* @param {string|object} steamAppId the steam app id of the game to lookup or a Giveaway object that was returned by the SgApi Giveaway Tools.
* @return {boolean} true, if the given game is on your wishlist
* @declared in sgapi_wishlist.js
*/
this.isWishlistedGame = function(steamAppId) {
if(!steamAppId){
return undefined;
}
if(steamAppId.steamAppId) //a ga-object was passed
steamAppId = steamAppId.steamAppId;
return self.get(steamAppId) !== undefined;
};
/**
*
* Alias for isWishlistedGame()
*
* @memberof SgApi.Wishlist.WishlistContainer
*
* @param {string|object} steamAppId the steam app id of the game to lookup or a Giveaway object that was returned by the SgApi Giveaway Tools.
* @return {boolean} true, if the given game is on your wishlist
* @declared in sgapi_wishlist.js
*/
this.contains = this.isWishlistedGame;
/**
*
* Get the wishlist entry for this game.
*
* @memberof SgApi.Wishlist.WishlistContainer
*
* @param {string|object} steamAppId the steam app id of the game to lookup or a Giveaway object that was returned by the SteamApi Giveaway Tools.
* @return {object} an object describing this giveaway entry
* @declared in sgapi_wishlist.js
*/
this.get = function(steamAppId){
if(!steamAppId)
return undefined;
if(steamAppId.steamAppId) //a ga-object was passed
steamAppId = steamAppId.steamAppId;
return list[steamAppId];
};
/**
*
* Get all Wishlist entries
*
* @memberof SgApi.Wishlist.WishlistContainer
*
* @return {Object} an object that has mapped the steamAppIds to their corresponding wishlist entries.
* @declared in sgapi_wishlist.js
*/
this.list = function(){
return list;
}
}
function getLastSyncedValue(){
return $(".nav__absolute-dropdown .nav__row__summary__description:contains(Last synced) span[data-timestamp]").attr("data-timestamp");
}
function needsSyncing(){
var savedVal = localStorage["sgapi__wishlist_lastSynced"] || "never";
var actualVal = getLastSyncedValue();
return savedVal !== actualVal;
}
function syncWishlist(){
var promise = $.Deferred();
$.ajax("//www.steamgifts.com/account/steam/wishlist").done(function(data){
var $data = $(data);
var numPages = Math.max.apply(null,$data.find(".pagination__navigation a[data-page-number]").map(function(){return parseInt(this.getAttribute("data-page-number"));}).get());
var wishlist = {};
var appendWishlistGames = function(data){
var $data = $(data);
$data.find(".table__row-inner-wrap").map(function(){
var $row =$(this);
var url = $row.find(" .table__column__secondary-link").attr("href");
var id = SgApi.Util.extractId(url);
var name = $row.find(".table__column__heading").text();
var since = $row.find(".table__column--width-small:has(span[data-timestamp]) span[data-timestamp]").attr("data-timestamp");
wishlist[id.toString()] = {url: url, appId: id, name: name, since:since };
});
};
appendWishlistGames(data);
var dfds = [];
for(var i=2; i<=numPages; i++){
dfds.push($.ajax("//www.steamgifts.com/account/steam/wishlist/search?page="+i).done(appendWishlistGames));
}
$.when.apply($, dfds).fail(promise.reject).done(function(){
localStorage["sgapi__wishlist"] = JSON.stringify(wishlist);
localStorage["sgapi__wishlist_lastSynced"] = getLastSyncedValue();
console.log("Wishlist synced successfully. Found "+Object.keys(wishlist).length+" games");
promise.resolve(new WishlistContainer(wishlist));
});
}).fail(promise.reject);
return promise;
}
function readWishlist(){
return new WishlistContainer(JSON.parse(localStorage["sgapi__wishlist"] || "{}"));
}
/**
*
* Get your wishlist. Since the internally cached copy of your wishlist needs to be synched sometimes, this function requires a success callback. The cache is used globally for all Userscripts, so synching should happen only happen once every few days after SG synched your account with steam.
* @memberof SgApi.Wishlist
* @param {function(WishlistContainer)} callback success callback that is invoked with your wishlist as parameter.
* @param {boolean} forceSync forces a synch of the internal cached wishlist. This is only relevant if somehow the cached version is corrupted, which hopefully won't ever happen.
* @declared in sgapi_wishlist.js
*/
this.get = function(callback, forceSync){
if(forceSync || needsSyncing()){
console.log("Start syncing wishlist");
syncWishlist().then(callback);
}else{
callback(readWishlist());
}
};
}();
});