export_subscriptions.js
· 4.3 KiB · JavaScript
Raw
// ==UserScript==
// @name Export Steam Workshop Subscriptions
// @version 1.0
// @description Adds a button to subscribed items page to easily export ALL workshop subscriptions to a simple JSON file.
// @author Jackz
// @match http*://steamcommunity.com/id/*/myworkshopfiles*
// @grant GM_setClipboard
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// ==/UserScript==
const ID_REGEX = new RegExp(/\?id=(\d+)/m);
(async function() {
'use strict';
if(getParameterByName('reset')) {
await GM_setValue('page', null)
await GM_setValue('items', null)
window.location.href = UpdateQueryString("reset", null, window.location.path)
return;
}
let page = await GM_getValue('page');
let items = await GM_getValue('items');
if(items) {
items = JSON.parse(items)
}else{
items = []
}
const isFirstTime = page == null
if(isFirstTime) {
page = parseInt(getParameterByName('p')) || 1
const forceEndPage = getParameterByName('end')
if(forceEndPage && !isNaN(forceEndPage)) {
await GM_setValue('lastpage', parseInt(forceEndPage))
}else{
const links = document.getElementsByClassName("pagelink")
const lastPage = links.length > 0 ? parseInt(links[links.length - 1].text) : 1
await GM_setValue('lastpage', lastPage)
}
await GM_setValue('items', null)
}
document.getElementsByClassName("rightDetailsBlock")[1].insert("<span class=\"btn_grey_steamui btn_medium\" id=\"subscribe-item\"><span>Export</span></span>")
document.getElementById('subscribe-item').addEventListener('click', () => grabItems(page, items))
if(!isFirstTime) {
await grabItems(page, items)
}
})();
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
hash;
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null) {
return url.replace(re, '$1' + key + "=" + value + '$2$3');
}
else {
hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?';
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
}
else {
return url;
}
}
}
async function grabItems(page, items) {
for(const item of document.getElementsByClassName("workshopItemSubscriptionDetails")) {
const match = item.outerHTML.match(ID_REGEX)
const name = item.childNodes[1].innerText
items.push({
fileid: match[1],
name: name.replace(/\s/g, '%20')
})
}
let lastPage = GM_getValue('lastpage')
if(page >= lastPage) {
await GM_setClipboard(items, "text");
await GM_setValue('page', null)
await GM_setValue('items', null)
const appid = getParameterByName("appid")
var a = document.createElement("a");
a.href = "data:application/json," + JSON.stringify({
items,
meta: {
timestamp: Date.now(),
appid
}
}); //content
a.download = "workshop_subscriptions_" + appid + ".json"; //file name
a.click();
return;
}else{
await GM_setValue('page', page + 1)
await GM_setValue('items', JSON.stringify(items))
const url = UpdateQueryString("p", page+1, window.location.path)
window.location.href = url
}
}
function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
1 | // ==UserScript== |
2 | // @name Export Steam Workshop Subscriptions |
3 | // @version 1.0 |
4 | // @description Adds a button to subscribed items page to easily export ALL workshop subscriptions to a simple JSON file. |
5 | // @author Jackz |
6 | // @match http*://steamcommunity.com/id/*/myworkshopfiles* |
7 | // @grant GM_setClipboard |
8 | // @grant GM_getValue |
9 | // @grant GM_setValue |
10 | // @grant GM_listValues |
11 | // ==/UserScript== |
12 | const ID_REGEX = new RegExp(/\?id=(\d+)/m); |
13 | (async function() { |
14 | 'use strict'; |
15 | if(getParameterByName('reset')) { |
16 | await GM_setValue('page', null) |
17 | await GM_setValue('items', null) |
18 | window.location.href = UpdateQueryString("reset", null, window.location.path) |
19 | return; |
20 | } |
21 | let page = await GM_getValue('page'); |
22 | let items = await GM_getValue('items'); |
23 | if(items) { |
24 | items = JSON.parse(items) |
25 | }else{ |
26 | items = [] |
27 | } |
28 | const isFirstTime = page == null |
29 | if(isFirstTime) { |
30 | page = parseInt(getParameterByName('p')) || 1 |
31 | const forceEndPage = getParameterByName('end') |
32 | if(forceEndPage && !isNaN(forceEndPage)) { |
33 | await GM_setValue('lastpage', parseInt(forceEndPage)) |
34 | }else{ |
35 | const links = document.getElementsByClassName("pagelink") |
36 | const lastPage = links.length > 0 ? parseInt(links[links.length - 1].text) : 1 |
37 | await GM_setValue('lastpage', lastPage) |
38 | } |
39 | await GM_setValue('items', null) |
40 | } |
41 | document.getElementsByClassName("rightDetailsBlock")[1].insert("<span class=\"btn_grey_steamui btn_medium\" id=\"subscribe-item\"><span>Export</span></span>") |
42 | document.getElementById('subscribe-item').addEventListener('click', () => grabItems(page, items)) |
43 | if(!isFirstTime) { |
44 | await grabItems(page, items) |
45 | } |
46 | })(); |
47 | |
48 | function UpdateQueryString(key, value, url) { |
49 | if (!url) url = window.location.href; |
50 | var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"), |
51 | hash; |
52 | |
53 | if (re.test(url)) { |
54 | if (typeof value !== 'undefined' && value !== null) { |
55 | return url.replace(re, '$1' + key + "=" + value + '$2$3'); |
56 | } |
57 | else { |
58 | hash = url.split('#'); |
59 | url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, ''); |
60 | if (typeof hash[1] !== 'undefined' && hash[1] !== null) { |
61 | url += '#' + hash[1]; |
62 | } |
63 | return url; |
64 | } |
65 | } |
66 | else { |
67 | if (typeof value !== 'undefined' && value !== null) { |
68 | var separator = url.indexOf('?') !== -1 ? '&' : '?'; |
69 | hash = url.split('#'); |
70 | url = hash[0] + separator + key + '=' + value; |
71 | if (typeof hash[1] !== 'undefined' && hash[1] !== null) { |
72 | url += '#' + hash[1]; |
73 | } |
74 | return url; |
75 | } |
76 | else { |
77 | return url; |
78 | } |
79 | } |
80 | } |
81 | |
82 | async function grabItems(page, items) { |
83 | for(const item of document.getElementsByClassName("workshopItemSubscriptionDetails")) { |
84 | const match = item.outerHTML.match(ID_REGEX) |
85 | const name = item.childNodes[1].innerText |
86 | items.push({ |
87 | fileid: match[1], |
88 | name: name.replace(/\s/g, '%20') |
89 | }) |
90 | } |
91 | let lastPage = GM_getValue('lastpage') |
92 | if(page >= lastPage) { |
93 | await GM_setClipboard(items, "text"); |
94 | await GM_setValue('page', null) |
95 | await GM_setValue('items', null) |
96 | const appid = getParameterByName("appid") |
97 | var a = document.createElement("a"); |
98 | a.href = "data:application/json," + JSON.stringify({ |
99 | items, |
100 | meta: { |
101 | timestamp: Date.now(), |
102 | appid |
103 | } |
104 | }); //content |
105 | a.download = "workshop_subscriptions_" + appid + ".json"; //file name |
106 | a.click(); |
107 | return; |
108 | }else{ |
109 | await GM_setValue('page', page + 1) |
110 | await GM_setValue('items', JSON.stringify(items)) |
111 | const url = UpdateQueryString("p", page+1, window.location.path) |
112 | window.location.href = url |
113 | } |
114 | } |
115 | |
116 | function getParameterByName(name, url = window.location.href) { |
117 | name = name.replace(/[\[\]]/g, '\\$&'); |
118 | var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), |
119 | results = regex.exec(url); |
120 | if (!results) return null; |
121 | if (!results[2]) return ''; |
122 | return decodeURIComponent(results[2].replace(/\+/g, ' ')); |
123 | } |