Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active March 9, 2021 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/4f3d3cc53ce1130f4ed6c9a108877904 to your computer and use it in GitHub Desktop.
Save unitycoder/4f3d3cc53ce1130f4ed6c9a108877904 to your computer and use it in GitHub Desktop.
GreaseMonkey script: Add Middle Mouse Button Support into Asset Store Notifications Window Panel
// ==UserScript==
// @name Add Middle Mouse Button support to Notification list
// @namespace https://unitycoder.com
// @version 1
// @include https://assetstore.unity.com/*
// @run-at document-end
// @grant none
// ==/UserScript==
// info https://unitycoder.com/blog/2021/03/09/add-middle-mouse-button-support-to-assetstore-notifications-panel-greasemonkey-script/
// code to inject into page (otherwise doesnt work)
var actualCode = '(' + function()
{
// dictionary to keep key image names and fixed urls
window.keys = new Object();
var proxied = window.XMLHttpRequest.prototype.open;
console.log("[GreaseMonkey] Override XMLHttpRequest");
window.XMLHttpRequest.prototype.open = function()
{
if (arguments.length>2 && arguments[1]=="/api/graphql/batch")
{
window.keys = new Object();
this.removeEventListener("load", HTTPTransferComplete);
this.addEventListener("load", HTTPTransferComplete);
}
return proxied.apply(this,[].slice.call(arguments));
};
// XMLHttpRequest finished
function HTTPTransferComplete(evt)
{
// continue only if its feed data
if (evt.target.response.indexOf("UserFeeds")==-1 || evt.target.response.indexOf("feed")==-1 || evt.target.response.indexOf("pages")==-1) return;
console.log("[GreaseMonkey] XMLHttpRequest catch complete");
// parse received data
var json = JSON.parse(evt.target.response);
// get only results data
var d = json[0]["data"]["currentUser"]["feed"]["results"];
// loop results, collect key and url
for(var i=0;i<d.length;i++)
{
// get key image data
var img = d[i]["htmlImage"]
// parse image name only
var key = img.substring(img.lastIndexOf("/") + 1,img.lastIndexOf("\'"));
// get div body html
var rawURL = d[i]["htmlBody"]
// get only slug and asset id
var temp = rawURL.match(/slug\/[0-9]+/gmi);
// craft new url
var realURL = "https://assetstore.unity.com/packages/" + temp[0]; //.split("/")[1];
// save into keys dictionary
window.keys[key] = realURL;
// console.log(key+" : "+realURL);
}
// FIXME timer to inject middle mouse click events into generated notification list divs.. should do this when list is build ready
setTimeout(AddMiddleMouseEvents, 2000);
}
function AddMiddleMouseEvents()
{
// add custom middle button clicks to div elements
var d1 = document.getElementsByClassName("_1JCO3 _2hiMB");
var d2 = document.getElementsByClassName("dkz6w _2hiMB");
// combine found htmlcollections
var d = [].concat([].slice.call(d1),[].slice.call(d2));
// loop divs
for(var i=0;i<d.length;i++)
{
d[i].addEventListener('auxclick', function (event)
{
// get key for this item and add that as link
var key = this.innerHTML.substring(this.innerHTML.lastIndexOf("key-image/") + 10,this.innerHTML.lastIndexOf(".png")+4);
var win = window.open(window.keys[key], '_blank');
win.focus();
});
}
console.log("[GreaseMonkey] MiddleMouseButton fix done!");
}
} + ')();';
// inject script to page
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment