- // ==UserScript==
- // @name Steam Key Helper
- // @namespace http://tampermonkey.net/
- // @version 1.0.0
- // @description try to take over the world!
- // @icon http://store.steampowered.com/favicon.ico
- // @author Bisumaruko
- // @include http*://*
- // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
- // @require https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.6/sweetalert2.min.js
- // @resource SweetAlert2CSS https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.6/sweetalert2.min.css
- // @connect store.steampowered.com
- // @grant GM_xmlhttpRequest
- // @grant GM_setValue
- // @grant GM_getValue
- // @grant GM_addStyle
- // @grant GM_getResourceText
- // @noframes
- // ==/UserScript==
-
- /* global GM_xmlhttpRequest, GM_setValue, GM_getValue, GM_addStyle, GM_getResourceText,
- swal, g_AccountID, g_sessionID,
- window, document, location */
-
- // setup jQuery
- const $ = jQuery.noConflict(true);
-
- // inject swal css
- GM_addStyle(GM_getResourceText('SweetAlert2CSS'));
-
- // setup swal
- swal.setDefaults({
- timer: 3000,
- useRejections: false
- });
-
- // inject CSS
- GM_addStyle(`
- .SKH_link {
- color: #57bae8;
- cursor: pointer;
- }
- .SKH_link:hover {
- text-decoration: underline;
- }
- .SKH_activated {
- text-decoration: line-through;
- }
- `);
-
- // load config
- const config = JSON.parse(GM_getValue('SKH_config') || '{}');
- const activated = JSON.parse(GM_getValue('SKH_activated') || '[]');
- const excludedTag = ['SCRIPT', 'STYLE', 'IFRAME', 'CANVAS'];
- const regKey = /([A-Za-z0-9]{5}-){2,4}[A-Za-z0-9]{5}/g;
-
- // functions
- const has = Object.prototype.hasOwnProperty;
- const updateActivated = (key, result) => {
- if (!activated.includes(key)) {
- if (result.success === 1 || [14, 15, 9].includes(result.purchase_result_details)) {
- activated.push(key);
- GM_setValue('SKH_activated', JSON.stringify(activated));
- $(`span:contains(${key})`).addClass('SKH_activated');
- }
- }
- };
- const getResultMsg = result => {
- const errMsg = {
- title: 'Opps!',
- html: 'An unexpected error has occured, please try again later',
- type: 'error'
- };
- const errors = {
- 14: 'Invalid Key',
- 15: 'Used Key',
- 53: 'Rate Limited',
- 13: 'Country Restricted',
- 9: 'Product Already Owned',
- 24: 'Missing Base Game',
- 36: 'PS3 Activation Required',
- 50: 'Gift Card/Wallet Code Detected'
- };
- const getDetails = items => {
- const details = [];
-
- items.forEach(item => {
- const detail = [item.line_item_description];
- if (item.packageid > 0) detail.push(`sub: ${item.packageid}`);
- if (item.appid > 0) detail.push(`app: ${item.appid}`);
-
- details.push(detail.join(', '));
- });
-
- return details.join('<br>');
- };
-
- if (result.success === 1) {
- return {
- title: 'Activation Successful!',
- html: getDetails(result.purchase_receipt_info.line_items),
- type: 'success'
- };
- } else if (result.success === 2) {
- if (has.call(errors, result.purchase_result_details)) {
- errMsg.html = errors[result.purchase_result_details];
- }
- if (result.purchase_receipt_info.line_items.length > 0) {
- errMsg.html += `<br>${getDetails(result.purchase_receipt_info.line_items)}`;
- }
- }
-
- return errMsg;
- };
- const activateKey = key => {
- swal({
- title: 'Nyaa~',
- text: 'Activating key, please wait',
- timer: null
- });
- swal.showLoading();
- GM_xmlhttpRequest({
- method: 'POST',
- url: 'https://store.steampowered.com/account/ajaxregisterkey/',
- headers: {
- Accept: 'text/javascript, text/html, application/xml, text/xml, */*',
- 'Accept-Encoding': 'gzip, deflate, br',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
- Origin: 'https://store.steampowered.com',
- Referer: 'https://store.steampowered.com/account/registerkey'
- },
- data: `product_key=${key}&sessionid=${config.sessionID}`,
- onload: res => {
- swal.close();
- if (res.status === 200) {
- try {
- const result = JSON.parse(res.response);
-
- swal(getResultMsg(result));
- updateActivated(key, result);
- } catch (e) {
- swal('Opps!', 'Result parse failed, please try again', 'error');
- }
- } else {
- swal({
- title: 'Opps!',
- html: 'Request failed, please try again<br>or update sessionID',
- type: 'error',
- timer: null,
- showCancelButton: true,
- preConfirm: () => {
- window.open('https://store.steampowered.com/');
- }
- });
- }
- }
- });
- };
- const generateLink = text => {
- const link = $(`<span class="SKH_link">${text}</span>`).click(() => {
- activateKey(text);
- });
- if (activated.includes(text)) link.addClass('SKH_activated');
-
- return link[0];
- };
- const scanText = text => {
- let matched = true;
- const matches = [];
-
- while (matched) {
- matched = regKey.exec(text.data);
- if (matched) matches.push(matched);
- }
-
- matches.reverse().forEach(match => {
- text.splitText(match.index);
- text.nextSibling.splitText(match[0].length);
- text.parentNode.replaceChild(generateLink(match[0]), text.nextSibling);
- });
- };
- const scanElement = element => {
- Array.from(element.childNodes).reverse().forEach(child => {
- if (child.nodeType === 1) {
- // element node
- if (child.type === 'text' && regKey.test(child.value)) {
- const $child = $(child);
-
- if (activated.includes(child.value)) $child.addClass('SKH_activated');
- $child.click(() => {
- activateKey(child.value);
- });
- } else if (!excludedTag.includes(child.tagName)) scanElement(child);
- } else if (child.nodeType === 3) {
- // text node
- scanText(child);
- }
- });
- };
- const init = () => {
- // save sessionID
- if (location.hostname === 'store.steampowered.com') {
- if (g_AccountID > 0) {
- if (config.sessionID !== g_sessionID) {
- config.sessionID = g_sessionID;
- GM_setValue('SBSE_config', JSON.stringify(config));
- }
- } else {
- swal('Not Logged-In', 'Please login to Steam so sessionID can be saved', 'error');
- }
- }
-
- // check sessionID
- if (!config.sessionID) {
- swal({
- title: 'Missing SessionID',
- text: 'Do you want to update your Steam sessionID?',
- type: 'question',
- timer: null,
- showCancelButton: true,
- preConfirm: () => {
- window.open('https://store.steampowered.com/');
- }
- });
- }
-
- scanElement(document.body);
- };
-
- $(init);