MAM Site Store Fix

Updates Ratio, FL, & Bonus Points in Store header after purchase (6/10/22)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name MAM Site Store Fix
// @namespace yyyzzz999
// @author yyyzzz999
// @description Updates Ratio, FL, & Bonus Points in Store header after purchase (6/10/22)
// @match  https://www.myanonamouse.net/store.php
// @icon   https://cdn.myanonamouse.net/imagebucket/164109/StoreFix.png
// @version 1.2
// @license MIT
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
/*jshint esversion: 6 */

// Problem: Site Store purchase updates the remaining bonus points in body of page, but not the header, or ratio
// even though the data has been sent to the JavaScript that processes the purchase.

// This saves a page reload to see your new ratio at the top of the page

let DEBUG =0; // Debugging mode on (1) or off (0) or 2 verbose, 3 Too verbose!

if (DEBUG) console.log("Store Fix 1.2 loaded"); // Just making sure we're watching the right console window

//https://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script
// Spy on MAM AJAX request/response to get hidden ratio and seedbonus updates not posted in page header
// This works in Basilisk Scratchpad, Tampermonkey w/Firefox, but not Greasmonkey 3.9 in Basilisk

let ResultObj; // I scoped this out of the function in case I want to reference it later in other contexts
(function(open) {
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener("readystatechange", function() {
        if (DEBUG >2) console.log(this.readyState);
		if (DEBUG >2) console.log(this.responseText);
			if (this.readyState == 4 && this.status == 200) {
				ResultObj = JSON.parse(this.responseText);
				if (DEBUG) console.log("New Ratio: " + ResultObj.ratio);
				if (DEBUG) console.log("Seedbonus: " + ResultObj.seedbonus);
				if (DEBUG >1) console.log("Response: " + this.responseText);
				if( ResultObj.ratio) {document.getElementById("tmR").textContent=ResultObj.ratio.toFixed(6);}
				if( ResultObj.seedbonus) document.getElementById("tmBP").textContent="Bonus: " + Math.floor(ResultObj.seedbonus);
			// The above two lines of code are similar to code I'd like to see in the MAM site.js.
                if( ResultObj.FLleft) {
                    let FLstat = document.querySelector("#tmFW");
                    if (FLstat !== null) { //If Preferences, Style, Main Menu, Top Menu, Freeleech Wedges [checked]
                    FLstat.textContent="FL Wedges: " + Math.floor(ResultObj.FLleft); //v1.2
                    }
                }
			}
        }, false);
        open.apply(this, arguments);
    };
})(XMLHttpRequest.prototype.open);