Skip to content

Commit

Permalink
3.1.0: major update for addon themes.
Browse files Browse the repository at this point in the history
(JS) new script: extensionStylesheetLoader.uc.js
this adds an attribute to the root element on all
addon documents, so userContent.css can select them.
this way you don't need to use @-moz-document url(...)
and can instead use :root[uc-extension-id="..."]
that's important because the addon's host name is randomized on install.
so now you'll be able to use my themes for various addons,
you won't have to edit the stylesheet every time you reinstall an addon,
and anyone else can share their own themes with this script too.
also update two scripts commensurately with this change.
miscMods.uc.js and urlbarMods.uc.js now add attributes
to chrome elements so that we can select them without extension URLs.
so this constitutes a pretty big improvement,
since those CSS rules previously weren't working for anyone except me.
now they'll work for everyone.
also update searchSelectionShortcut to eliminate a bug where
the script would occasionally create a "chrome" folder
in your devtools chrome_debugger_profile,
preventing our stylesheets from injecting into devtools.
make sure you go into your profile folder (roaming)
and delete chrome_debugger_profile if it exists.
update tabMods.uc.js to 1.3.1 to fix tab label height.
(CSS) refactor every addon theme to make them work with this new script.
refactor some other styles for the same goal.
fix tab label height.
add a stylesheet for mozilla websites.
(fonts) replace Overpass Mono with Fira Code.
see the attached commit notes for more changes.
  • Loading branch information
aminomancer committed Jan 7, 2022
1 parent 2f57fdb commit 6f4ca90
Show file tree
Hide file tree
Showing 30 changed files with 960 additions and 625 deletions.
108 changes: 108 additions & 0 deletions JS/extensionStylesheetLoader.uc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// ==UserScript==
// @name Extension Stylesheet Loader
// @version 1.0.0
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Allows users to share stylesheets for webextensions without needing to edit the
// URL. This works by creating an actor in every extension browser that sets an attribute on the
// root element to expose the addon's ID to user stylesheets. This means we can use the addon's ID
// instead of @-moz-document url(). That is good because addons' URLs are randomly generated upon
// install, meaning the URLs I specify in resources/in-content/ext-*.css will not be the same as
// yours, so they will not work for you. You can also use this in combination with my
// debugExtensionInToolbarContextMenu.uc.js to add your own style rules for extension content. Once
// you have that script installed, you can right-click an addon's toolbar button > Debug Extension >
// Copy ID. Then, in userContent.css, add a rule like
// :root[uc-extension-id="example@aminomancer"]{color:red} Keep in mind, the ID is not the same as
// the URL. That's why this script is necessary in the first place. URLs are random, unique, and
// per-install. Conversely, an extension's ID is permanent and universal, but potentially not
// unique, in that two authors could potentially make extensions with the same ID. I haven't seen
// this before but it's possible, in principle. If you need to, you can find the ID by navigating to
// about:debugging#/runtime/this-firefox
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// @include main
// @onlyonce
// ==/UserScript==

class ExtensionStylesheetLoader {
constructor() {
this.setup();
}
async setup() {
// make a temp directory for our child file
const registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
let tempDir = FileUtils.getFile("UChrm", [".ExtensionStylesheetLoader"]);
let { path } = tempDir;
await IOUtils.makeDirectory(path, { ignoreExisting: true, createAncestors: false });
// hide the temp dir on windows so it doesn't get in the way of user activities or prevent its eventual deletion.
if (AppConstants.platform === "win") {
if ("setWindowsAttributes" in IOUtils)
await IOUtils.setWindowsAttributes(path, { hidden: true });
}
this.tempPath = path;

// create a manifest file that registers a URI for chrome://uc-extensionstylesheetloader/content/
this.manifestFile = await this.createTempFile(`content uc-extensionstylesheetloader ./`, {
name: "ucsss",
type: "manifest",
});
this.childFile = await this.createTempFile(
`"use strict";const EXPORTED_SYMBOLS=["ExtensionStylesheetLoaderChild"];const{WebExtensionPolicy}=Cu.getGlobalForObject(Cu);class ExtensionStylesheetLoaderChild extends JSWindowActorChild{handleEvent(e){let policy=WebExtensionPolicy.getByHostname(this.document.location.hostname);if(policy&&policy.id)this.document.documentElement.setAttribute("uc-extension-id",policy.id)}}`,
{ name: "ExtensionStylesheetLoaderChild", type: "jsm" }
);

let manifest = FileUtils.getFile("UChrm", [
".ExtensionStylesheetLoader",
this.manifestFile.name,
]);
if (manifest.exists()) registrar.autoRegister(manifest);
else return;
ChromeUtils.registerWindowActor("ExtensionStylesheetLoader", {
child: {
moduleURI: this.childFile.url,
events: { DOMDocElementInserted: {} },
},
allFrames: true,
matches: ["moz-extension://*/*"],
messageManagerGroups: ["browsers", "webext-browsers", "sidebars"],
});
// listen for application quit so we can clean up the temp files.
Services.obs.addObserver(this, "quit-application");
}
/**
* create a file in the temp folder
* @param {string} contents (the actual file contents in UTF-8)
* @param {object} options (an optional object containing properties path or name. path creates a file at a specific absolute path. name creates a file of that name in the chrome/.ExtensionStylesheetLoader folder. if omitted, it will create chrome/.ExtensionStylesheetLoader/uc-temp)
* @returns {object} { name, url } (an object containing the filename and a chrome:// URL leading to the file)
*/
async createTempFile(contents, options = {}) {
let { path = null, name = "uc-temp", type = "txt" } = options;
const uuid = Cc["@mozilla.org/uuid-generator;1"]
.getService(Ci.nsIUUIDGenerator)
.generateUUID()
.toString();
name += "-" + uuid + "." + type;
if (!path) path = FileUtils.getFile("UChrm", [".ExtensionStylesheetLoader", name]).path;
await IOUtils.writeUTF8(path, contents);
let url = "chrome://uc-extensionstylesheetloader/content/" + name;
return { name, url };
}
// application quit listener. clean up the temp files.
observe(subject, topic, data) {
switch (topic) {
case "quit-application":
Services.obs.removeObserver(this, "quit-application");
this.cleanup();
break;
default:
}
}
// remove the temp directory when firefox's main process ends
async cleanup() {
await IOUtils.remove(this.tempPath, {
ignoreAbsent: true,
recursive: true,
});
}
}

if (location.href === AppConstants.BROWSER_CHROME_URL) new ExtensionStylesheetLoader();
20 changes: 19 additions & 1 deletion JS/miscMods.uc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Misc. Mods
// @version 1.9.1
// @version 1.9.2
// @author aminomancer
// @homepage https://github.com/aminomancer/uc.css.js
// @description Various tiny mods not worth making separate scripts for. Read the comments inside the script for details.
Expand Down Expand Up @@ -398,6 +398,24 @@
this._switcherPanel.openPopup(this._icon);
this._switcherTarget.classList.add("active");
};

// add an "engine" attribute to the searchbar autocomplete popup, so we can replace the
// engine icon with CSS without needing to use the (random) icon URL as a selector. this
// way, my replacing the built-in Google engine's icon with a nicer-looking one will
// work for everyone. otherwise we'd have problems since the URL is randomly generated
// for everyone. my selector wouldn't work for you. but here, we can make it universal:
// #PopupSearchAutoComplete[engine="Google"] .searchbar-engine-image
// see uc-urlbar.css for the implementation in duskFox.
eval(
`PopupSearchAutoComplete.updateHeader = async function ` +
PopupSearchAutoComplete.updateHeader
.toSource()
.replace(/async updateHeader/, "")
.replace(
/(let uri = engine.iconURI;)/,
`engine.name ? this.setAttribute("engine", engine.name) : this.removeAttribute("engine");\n $1`
)
);
}
}

Expand Down
Loading

0 comments on commit 6f4ca90

Please sign in to comment.