Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active June 15, 2023 16:52
Show Gist options
  • Save unitycoder/061b00201170399404a285175b2137dc to your computer and use it in GitHub Desktop.
Save unitycoder/061b00201170399404a285175b2137dc to your computer and use it in GitHub Desktop.
Unity Forum Search : AutoSelect current board item inside Search select list (based on referrer url)
// ==UserScript==
// @name Unity Forum Search : AutoSelect current board item inside Search select list (based on referrer url)
// @namespace https://unitycoder.com
// @version 1
// @include https://forum.unity.com/search/?type=post
// @grant none
// ==/UserScript==
// more info https://unitycoder.com/blog/2021/05/26/unity-forums-auto-select-current-subforum-in-search-greasemonkey-script/
// get referring board url
if (document.referrer==null) return;
// cleanup ref
var ref = document.referrer;
ref = ref.replace("https://forum.unity.com/forums/","");
// find board name from url
var dotPos = ref.indexOf('.');
if (dotPos==-1) return;
ref = ref.substring(0,dotPos);
// get selection box that contains forum boards
var sel = document.getElementById("ctrl_nodes");
// loop to find match in the select items and url board name
for (var i = 0;i<sel.options.length;i++)
{
var item = sel.options[i].text.trim();
if (item.toLowerCase() == ref.toLowerCase())
{
sel.selectedIndex = i;
break;
}
}
@instance-id
Copy link

instance-id commented Jun 15, 2023

This didn't work out of the gate for me in firefox, I had to make the following edit. After that, all was well.

https://gist.github.com/instance-id/737100c016acd206b399e90e3a907a27#file-unityforumboardsearchhelper-js-L20

Then also added the ability to be used from a 'categories' page

if (ref.includes("categories")) {
    ref = ref.replace("https://forum.unity.com/categories/", "");
} else if (ref.includes("forums")) {
    ref = ref.replace("https://forum.unity.com/forums/", "");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment