User:Andrewds1021/CategoryCSS

This solution was created in response to Thread:1507300 and debugged in response to Thread:1559477.

The purpose of this script is to apply a specific stylesheet based on the category membership of the current page. The script uses MediaWiki's api.php to check if the page belongs to one of the specified categories. If so, it imports the specified associated stylesheet.

Installation
The following instructions pertain to simply importing the script. If you wish, you may alternatively copy the script and maintain your own copy. Note that while any user may export (help page) the page, only local admins can import pages. Furthermore, local admins are not permitted to import pages into the MediaWiki namespace.

Installing this script for all users on a particular wiki (ie. wiki-wide) requires that the user performing the installation have sysop (ie. local administrator) privileges. Furthermore, the JS review process must be enabled. If the review process it not yet enabled, a request to enable it can be sent to staff using Special:Contact/general. If installing for personal use, personal JS must be enabled in order for the script to run. Personal JS can be enabled at Special:Preferences (under the section "Advanced display options").

To install, simply import the script using the appropriate JS page. The simplest way to do this is to copy and paste the following into the JS page. When installing wiki-wide, this should go in either  or   on the wiki for which the script is being installed. Currently, there is no difference between those two pages. When installing for personal use on a particular wiki, this should go in either  or   on the wiki for which the script is being installed. There is currently no significant difference between those two pages. Be sure to replace  with the actual username of the user for which the script is being installed. For example, if installing for yourself, you might place the import in. If installing for personal use on all wikis, this should go in your global JS.

Configuration
To set the list of categories to search for and the associated stylesheets, copy and paste the following into the appropriate JS page. Replace,  ,  , ... with the actual category page names (case sensitive but without the namespace). Similarly, replace,  ,  , ... with the actual CSS page names using the format required for. This ideally would be done immediately prior to the script import but could possibly be done somewhere else.

Example
The following should apply  to all pages in the Help category.  if (!window.andrewds1021) { window.andrewds1021 = { category_css: {} }; } else if (!window.andrewds1021.category_css) { window.andrewds1021.category_css = {}; } window.andrewds1021.category_css.category_styles = { "Help": "u:w:User:Andrewds1021/Test_CSS.css" }; importArticles({   type: "script",    article: "u:w:User:Andrewds1021/JS/CategoryCSS" });

Limitations/Additional Notes

 * 1) When creating stylesheets, the customization policy must be followed.
 * 2) The script accepts only one (1) stylesheet per category. If you need/want to apply multiple stylesheets to the same category, use @import.
 * 3) If the page belongs to more than one (1) category for which custom CSS has been assigned, only one (1) of the associated stylesheets will be applied. Based on the ambiguity of the JS specifications, the chosen category could be random. However, it is commonly believed that most modern browsers actually do have predictable output. Based on this, the applied stylesheet is likely to be that which is associated with the category which, amongst the matched categories, appears first in the category list.