-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSteamPackages.user.js
More file actions
196 lines (170 loc) · 7.15 KB
/
SteamPackages.user.js
File metadata and controls
196 lines (170 loc) · 7.15 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// ==UserScript==
// @name Free Steam Packages Redeemer
// @version 1.0.1
// @author Dillon Regimbal
// @namespace https://dillonr.com
// @description Automates sending requests to Steam. Get package list from https://steamdb.info/freepackages/
// @match https://store.steampowered.com/account/licenses/
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
// Since 2020-05-21
// https://greasyfork.org/en/scripts/403894-free-steam-packages-redeemer
// https://github.com/dregimbal/UserScripts/blob/master/SteamPackages.user.js
// To display the entire list (instead of just 50) at SteamDB, use the following style
// #freepackages.package { display: block }
/* eslint-disable no-extend-native */
/* eslint-disable no-param-reassign */
/* eslint-disable no-unused-vars */
(function () {
// Uncomment the following line, and replace the array with your package list
// GM_setValue('steamPackages', [{ id: 0, name: 'Package 1' },{ id: 0, name: 'Package 2' }]])
let steamPackages = GM_getValue('steamPackages', [{ id: 0, name: 'No Package' }])
let completedPackages = GM_getValue('completedPackages', [{ id: 0, name: 'No Package' }])
let errorPackages = GM_getValue('errorPackages', [{ id: 0, name: 'No Package' }])
let currentPackage = GM_getValue('currentPackage', null)
console.log(completedPackages)
console.log(errorPackages)
console.log(currentPackage)
let failCount = 0
let modal
let modalTitle = ''
let modalText = ''
Date.prototype.addHours = function (h) {
this.setTime(this.getTime() + (h * 60 * 60 * 1000))
return this
}
Date.prototype.addMinutes = function (m) {
this.setTime(this.getTime() + (m * 60 * 1000))
return this
}
Date.prototype.addSeconds = function (s) {
this.setTime(this.getTime() + (s * 1000))
return this
}
function showModal(title, text) {
if (modal) {
modal.Dismiss()
}
if (title === '') {
title = modalTitle
}
if (text === '') {
text = modalText
}
modal = unsafeWindow.ShowBlockingWaitDialog(title, '<br />' + text)
}
function callDelayed(hours, minutes, seconds, delayedFunction, param1) {
let now = new Date()
let next = new Date().addHours(hours).addMinutes(minutes).addSeconds(seconds)
let difference = next - now
let timeCount = ''
if (hours > 0) {
if ((minutes > 0) && (seconds > 0)) {
timeCount = `${hours} hours, ${minutes} minutes, ${seconds} seconds`
} else if ((minutes > 0)) {
timeCount = `${hours} hours, ${minutes} minutes`
} else if ((seconds > 0)) {
timeCount = `${hours} hours, ${seconds} seconds`
} else {
timeCount = `${hours} hours`
}
} else if (minutes > 0) {
if (seconds > 0) {
timeCount = `${minutes} minutes, ${seconds} seconds`
} else {
timeCount = `${minutes} minutes`
}
} else {
timeCount = `${seconds} seconds`
}
// console.log('Current time: ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '.' + now.getMilliseconds())
// console.log('Next run time: ' + next.getHours() + ':' + next.getMinutes() + ':' + next.getSeconds() + '.' + next.getMilliseconds())
let options = {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'America/Toronto'
}
let datestring = next.toLocaleTimeString('en-CA', options)
showModal('', `${modalText}<br />Next function will run in ${timeCount} at <b>${datestring}</b>.`)
unsafeWindow.freeTimeout = unsafeWindow.setTimeout(function () {
if (typeof param1 !== 'undefined') {
delayedFunction(param1)
} else {
delayedFunction()
}
}, difference)
}
function getPackageStats() {
let total = steamPackages.length + completedPackages.length + errorPackages.length
let stats = ''
if (currentPackage !== null) {
stats = `Current ID: [${currentPackage.id}]<br />Package Name: [${currentPackage.name}]<br />`
}
stats += `Completed: ${completedPackages.length}<br />Errors: ${errorPackages.length}<br />Total: ${total}`
return stats
}
const saveValueAndReload = () => {
GM_setValue('currentPackage', currentPackage)
GM_setValue('errorPackages', errorPackages)
GM_setValue('completedPackages', completedPackages)
GM_setValue('steamPackages', steamPackages)
location.reload()
}
function fetch(steamPackage) {
jQuery.post(
'https://store.steampowered.com/checkout/addfreelicense/' + steamPackage.id,
{
ajax: true,
sessionid: unsafeWindow.g_sessionID
})
.done((data, textStatus, jqXHR) => {
modalTitle = 'Request Success'
modalText = `${getPackageStats()}`
showModal(modalTitle, modalText)
completedPackages.push(steamPackage)
GM_setValue('completedPackages', completedPackages)
currentPackage = null
GM_setValue('currentPackage', null)
GM_setValue('steamPackages', steamPackages)
callDelayed(0, 0, 2, requestNext)
})
.fail((jqXHR, textStatus, errorThrown) => {
failCount++
modalText = `Received error on last request<br />Fails: ${failCount}<br />${jqXHR.status}: ${jqXHR.statusText}<br />${getPackageStats()}`
if (failCount < 2) {
modalTitle = 'Waiting to retry package'
showModal(modalTitle, modalText)
callDelayed(0, 0, 15, fetch, steamPackage)
} else {
modalTitle = 'Waiting to refresh'
showModal(modalTitle, modalText)
errorPackages.push(steamPackage)
GM_setValue('errorPackages', errorPackages)
currentPackage = null
GM_setValue('currentPackage', null)
GM_setValue('steamPackages', steamPackages)
callDelayed(0, 17, 0, saveValueAndReload)
}
})
}
function requestNext() {
if (modal) {
modal.Dismiss()
}
if (typeof currentPackage === 'undefined' || currentPackage === null) {
currentPackage = steamPackages.shift()
GM_setValue('currentPackage', currentPackage)
}
modalTitle = 'Executing request'
modalText = `${getPackageStats()}`
showModal(modalTitle, modalText)
fetch(currentPackage)
}
modalTitle = 'Waiting to send requests'
modalText = `g_sessionID: ${unsafeWindow.g_sessionID}<br />${getPackageStats()}`
showModal(modalTitle, modalText)
callDelayed(0, 0, 10, requestNext)
}())