Skip to content

Instantly share code, notes, and snippets.

@gb96
Last active September 26, 2019 07:15
Show Gist options
  • Save gb96/5f636e44c64b81d2e49c615016eea167 to your computer and use it in GitHub Desktop.
Save gb96/5f636e44c64b81d2e49c615016eea167 to your computer and use it in GitHub Desktop.
UserScript for Google Play Console Order management form. Selects items for refund based on a hard-coded filter
// ==UserScript==
// @name Google Dev Select Orders to Refund
// @namespace https://hypearth.com/
// @version 1.16
// @description Google Play Console Order management form: Selects items for refund based on a hard-coded filter. Adds a Page+10 button
// @author gerg.bowering
// @include https://play.google.com/apps/publish/*
// @grant none
// ==/UserScript==
// FIXME @run-at document-idle
(function() {
'use strict';
var skuToRefund = 'tier1_monthly_standard'; // TODO make this a labelled text inputSelect
// Button selects rows matching skuToRefund in state Charged that have a non-zero fee
var inputSelect = document.createElement("input");
inputSelect.value = "Select";
// Button moves 10 pages forward through results
var inputPage10 = document.createElement("input");
inputPage10.value = "+10 >>";
// Button moves 50 pages forward through results
var inputPage50 = document.createElement("input");
inputPage50.value = "+50 >>";
var isButtonAdded = false;
var checkedCount = 0;
var clickSubsToRefund = function(e) {
var checked = (inputSelect.value === 'Select');
let rowClassNames = ['GNVPVGB-O-d', 'GNVPVGB-O-t'];
for (let c in rowClassNames) {
let className = rowClassNames[c];
let rows = document.getElementsByClassName(className);
if (!rows || !rows.length) {
console.log("ERROR ClassName " + className + " matched no rows!");
continue;
}
console.log("ClassName " + className + " matched " + rows.length + " rows");
for (let i=0; i < rows.length; i++) {
let row = rows[i];
let cb = row.querySelector("td > div > input[type=checkbox]");
let orderId = row.children[1].querySelector("a").innerHTML.trim();
let sku = row.querySelector("td:nth-child(4) > div > div > div:nth-child(2) > div > div").innerHTML;
let status = row.children[4].querySelector("div > div").innerHTML.trim();
let fee = row.children[5].querySelector("div").innerHTML.trim();
if (sku === skuToRefund && status === "Charged" && fee.match(/[1-9]/) != null) {
// cb.style.backgroundColor='#FFFF00';
// cb.style.borderColor='#FF3333';
cb.click();
if (checked) {
checkedCount++;
} else {
checkedCount--;
}
console.log(inputSelect.value + 'ed orderId ' + orderId + ' count=' + checkedCount);
}
}
}
if (inputSelect.value === 'Select' && checkedCount > 0) {
inputSelect.value = 'Deselect';
} else {
inputSelect.value = 'Select';
checkedCount = 0;
}
};
var formatCss = function(top, left) {
let css = ""
+ "border: none;"
+ "border-radius: 2px;"
+ "box-sizing: border-box;"
+ "font: 500 14px Roboto,RobotoDraft,Helvetica,Arial,sans-serif;"
+ "position: absolute;"
+ "text-align: center;"
+ "text-transform: uppercase;"
+ "vertical-align: middle;"
+ "color: white;"
+ "background-color: #0277bd;"
+ "box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2);"
+ "top: " + top + "px;"
+ "left: " + left + "px;"
+ "margin: 9px;"
+ "padding: 9px;"
+ "z-index: 9999;"
+ "transition-property: background-color,box-shadow,color,opacity;"
+ "transition-duration: .4s;"
+ "transition-timing-function: cubic-bezier(0.25,0.8,0.25,1);"
return css;
};
var styleButton = function(btn, top, left) {
console.log("styleButton " + btn.value + ", " + top + ", " + left);
if (!btn) return;
var btnStyle = btn.style;
if (!btnStyle) return;
btnStyle["border"] = "none";
btnStyle["border-radius"] = "2px";
btnStyle["font"] = "500 14px Roboto,RobotoDraft,Helvetica,Arial,sans-serif";
btnStyle["position"] = "absolute";
btnStyle["text-align"] = "center";
btnStyle["text-transform"] = "uppercase";
btnStyle["vertical-align"] = "middle";
btnStyle["color"] = "white";
btnStyle["background-color"] = "#0277bd";
btnStyle["box-shadow"] = "0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2)";
btnStyle["top"] = "" + top + "px";
btnStyle["left"] = "" + left + "px";
btnStyle["margin"] = "9px";
btnStyle["padding"] = "9px";
btnStyle["z-index"] = "9999";
btnStyle["transition-property"] = "background-color,box-shadow,color,opacity";
btnStyle["transition-duration"] = "0.4s";
btnStyle["transition-timing-function"] = "cubic-bezier(0.25,0.8,0.25,1)";
};
// Find a page of results by clicking next until the page number matches...
var nextPage = function (findPage) {
if (document.readyState === "complete") {
// console.log('nextPage(' + findPage + ')');
var currentPage = parseInt(document.querySelector("body > div:nth-child(5) > div > div:nth-child(2) > div > div:nth-child(2) > div > div > div > div > div > div:nth-child(1) > div > div > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(2) > div > div > span > strong:nth-child(1)").innerText, 10);
console.log('nextPage(' + findPage + ') currentPage=' + currentPage);
if (currentPage >= findPage) return;
var nextBtn = document.querySelector("#gwt-uid-293 > aside");
if (!nextBtn) {
console.log('nextPage(' + findPage + ') Could not find the next page button!');
return;
}
nextBtn.click();
}
setTimeout(nextPage, 1000, findPage);
};
// Skip 10 pages forward
var nextPage10 = function() {
// console.log('nextPage10()');
// var currentPage = parseInt(document.querySelector("body > div:nth-child(5) > div > div:nth-child(2) > div > div:nth-child(2) > div > div.GNVPVGB-T-c > div > div.GNVPVGB-G-m > div > div:nth-child(1) > div > div > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(2) > div.GNVPVGB-si-e > div > span > strong:nth-child(1)").innerText, 10);
var currentPage = parseInt(document.querySelector("body > div:nth-child(5) > div > div:nth-child(2) > div > div:nth-child(2) > div > div > div > div > div > div:nth-child(1) > div > div > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(2) > div > div > span > strong:nth-child(1)").innerText, 10);
console.log('nextPage10() currentPage=' + currentPage);
if (currentPage) nextPage(currentPage + 10);
};
// Skip 50 pages forward
var nextPage50 = function() {
var currentPage = parseInt(document.querySelector("body > div:nth-child(5) > div > div:nth-child(2) > div > div:nth-child(2) > div > div > div > div > div > div:nth-child(1) > div > div > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(2) > div > div > span > strong:nth-child(1)").innerText, 10);
console.log('nextPage50() currentPage=' + currentPage);
if (currentPage) nextPage(currentPage + 50);
};
// Position new buttons on the page
var positionButton = function() {
var tableHeader = document.querySelector("body > div:nth-child(5) > div > div:nth-child(2) > div > div:nth-child(2) > div > div > div > div > div > div:nth-child(1) > div > div > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(3) > div > table > thead > tr > th:nth-child(3)");
if (!tableHeader) {
console.log('Could not find the tableHeader!');
setTimeout(positionButton, 2000);
if (isButtonAdded) {
isButtonAdded = false;
inputSelect.style.visibility = "hidden";
inputPage10.style.visibility = "hidden";
}
return;
}
var rect = tableHeader.getClientRects()[0];
let top = Math.round(rect.top - 54);
let left = Math.round(rect.left + 30);
inputSelect.type = "button";
inputSelect.onclick = clickSubsToRefund;
inputSelect.onmouseover = function() {inputSelect.style.backgroundColor='#0266b0'; return true;};
inputSelect.onmouseout = function() {inputSelect.style.backgroundColor='#0277bd'; return true;};
styleButton(inputSelect, top, left);
inputPage10.type = "button";
inputPage10.onclick = nextPage10;
inputPage10.onmouseover = function() {inputPage10.style.backgroundColor='#0266b0'; return true;};
inputPage10.onmouseout = function() {inputPage10.style.backgroundColor='#0277bd'; return true;};
left += 100;
styleButton(inputPage10, top, left);
inputPage50.type = "button";
inputPage50.onclick = nextPage50;
inputPage50.onmouseover = function() {inputPage10.style.backgroundColor='#0266b0'; return true;};
inputPage50.onmouseout = function() {inputPage50.style.backgroundColor='#0277bd'; return true;};
left += 100;
styleButton(inputPage50, top, left);
if (!isButtonAdded) {
document.body.appendChild(inputSelect);
inputSelect.style.visibility = "visible";
document.body.appendChild(inputPage10);
inputPage10.style.visibility = "visible";
document.body.appendChild(inputPage50);
inputPage50.style.visibility = "visible";
isButtonAdded = true;
}
}
// Window Resize event handler
var onWinResize = function() {
if (!isButtonAdded) return;
positionButton();
};
// Handler when the DOM is fully loaded
var callback = function(){
if (document.location.href.indexOf("#OrderManagementPlace") != -1 && document.location.href.indexOf("orderid=") == -1) {
setTimeout(positionButton, 1000);
window.addEventListener("resize", onWinResize);
} else if (isButtonAdded) {
isButtonAdded = false;
inputSelect.style.visibility = "hidden";
inputPage10.style.visibility = "hidden";
inputPage50.style.visibility = "hidden";
}
};
// Wait for document loading to be complete before doing anything
if (
document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
callback();
} else {
document.addEventListener("DOMContentLoaded", callback);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment