2023-04-06 13:16:05 +00:00
|
|
|
// Copyright 2023 Arnaud Ferraris, Oliver Smith
|
2022-01-30 10:48:42 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-08-20 11:20:42 +00:00
|
|
|
// This is a Firefox autoconfig file:
|
|
|
|
// https://support.mozilla.org/en-US/kb/customizing-firefox-using-autoconfig
|
|
|
|
// Import custom userChrome.css on startup or new profile creation
|
2023-04-06 13:16:05 +00:00
|
|
|
// Log file: $(find ~/.mozilla -name mobile-config-firefox.log)
|
|
|
|
|
2020-08-20 11:20:42 +00:00
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
2022-02-17 16:17:13 +00:00
|
|
|
var updated = false;
|
|
|
|
|
2020-08-20 11:20:42 +00:00
|
|
|
// Create <profile>/chrome/ directory if not already present
|
|
|
|
var chromeDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
|
|
|
chromeDir.append("chrome");
|
|
|
|
if (!chromeDir.exists()) {
|
2021-12-04 15:36:56 +00:00
|
|
|
chromeDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
|
2020-08-20 11:20:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:16:05 +00:00
|
|
|
var logFile = chromeDir.clone();
|
|
|
|
logFile.append("mobile-config-firefox.log");
|
|
|
|
var mode = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND;
|
|
|
|
var logFileStream = FileUtils.openFileOutputStream(logFile, mode);
|
|
|
|
|
|
|
|
function log(line) {
|
|
|
|
var date = new Date().toISOString().replace("T", " ").slice(0, 19);
|
|
|
|
line = "[" + date + "] " + line + "\n";
|
|
|
|
logFileStream.write(line, line.length);
|
|
|
|
}
|
|
|
|
|
2023-04-06 16:54:07 +00:00
|
|
|
// Debug function for logging object attributes
|
|
|
|
function log_obj(obj) {
|
|
|
|
var prop;
|
|
|
|
var value;
|
|
|
|
|
|
|
|
for (var prop in obj) {
|
|
|
|
try {
|
|
|
|
value = obj[prop];
|
|
|
|
} catch(e) {
|
|
|
|
value = e;
|
|
|
|
}
|
|
|
|
log(" - " + prop + ": " + value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 16:54:59 +00:00
|
|
|
function get_firefox_version() {
|
|
|
|
try {
|
|
|
|
return Services.appinfo.lastAppVersion;
|
|
|
|
} catch(e) {
|
|
|
|
log("Failed to get FF version: " + e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 14:48:55 +00:00
|
|
|
function trigger_firefox_restart() {
|
|
|
|
log("Triggering Firefox restart");
|
|
|
|
var appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
|
|
|
|
appStartup.quit(Ci.nsIAppStartup.eForceQuit | Ci.nsIAppStartup.eRestart);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_default_prefs() {
|
|
|
|
log("Setting default preferences");
|
|
|
|
// Select a mobile user agent for firefox (same as tor browser on android)
|
|
|
|
defaultPref('general.useragent.override', 'Mozilla/5.0 (Android 10; Mobile; rv:110.0) Gecko/110.0 Firefox/110.0');
|
|
|
|
|
|
|
|
// Do not suggest facebook, ebay, reddit etc. in the urlbar. Same as
|
|
|
|
// Settings -> Privacy & Security -> Address Bar -> Shortcuts. As
|
|
|
|
// side-effect, the urlbar results are not immediatelly opened once
|
|
|
|
// clicking the urlbar.
|
|
|
|
defaultPref('browser.urlbar.suggest.topsites', false);
|
|
|
|
|
|
|
|
// Do not suggest search engines. Even though amazon is removed via
|
|
|
|
// policies.json, it gets installed shortly after the browser is opened.
|
|
|
|
// With this option, at least there is no big "Search with Amazon" message
|
|
|
|
// in the urlbar results as soon as typing the letter "a".
|
|
|
|
defaultPref('browser.urlbar.suggest.engines', false);
|
|
|
|
|
|
|
|
// Show about:home in new tabs, so it's not just a weird looking completely
|
|
|
|
// empty page.
|
|
|
|
defaultPref('browser.newtabpage.enabled', true);
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:16:05 +00:00
|
|
|
log("Running mobile-config-autoconfig.js");
|
|
|
|
|
2023-04-06 16:54:59 +00:00
|
|
|
var ff_version = get_firefox_version();
|
|
|
|
log("Firefox version: " + ff_version);
|
|
|
|
|
2020-08-20 11:20:42 +00:00
|
|
|
// Create nsIFile objects for userChrome.css in <profile>/chrome/ and in /etc/
|
|
|
|
var chromeFile = chromeDir.clone();
|
|
|
|
chromeFile.append("userChrome.css");
|
|
|
|
var defaultChrome = new FileUtils.File("/etc/mobile-config-firefox/userChrome.css");
|
|
|
|
|
|
|
|
// Remove the existing userChrome.css if older than the installed one
|
|
|
|
if (chromeFile.exists() && defaultChrome.exists() &&
|
2023-04-06 13:44:01 +00:00
|
|
|
chromeFile.lastModifiedTime < defaultChrome.lastModifiedTime) {
|
2023-04-06 13:16:05 +00:00
|
|
|
log("Removing outdated userChrome.css from profile");
|
2021-12-04 15:36:56 +00:00
|
|
|
chromeFile.remove(false);
|
2020-08-20 11:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy userChrome.css to <profile>/chrome/
|
|
|
|
if (!chromeFile.exists()) {
|
2023-04-06 13:16:05 +00:00
|
|
|
log("Copying userChrome.css from /etc/mobile-config-firefox to profile");
|
2021-12-04 15:36:56 +00:00
|
|
|
defaultChrome.copyTo(chromeDir, "userChrome.css");
|
2022-02-17 16:17:13 +00:00
|
|
|
updated = true;
|
2020-08-20 11:20:42 +00:00
|
|
|
}
|
2021-05-15 18:33:29 +00:00
|
|
|
|
|
|
|
// Create nsIFile objects for userContent.css in <profile>/chrome/ and in /etc/
|
|
|
|
var contentFile = chromeDir.clone();
|
|
|
|
contentFile.append("userContent.css");
|
|
|
|
var defaultContent = new FileUtils.File("/etc/mobile-config-firefox/userContent.css");
|
|
|
|
|
|
|
|
// Remove the existing userContent.css if older than the installed one
|
|
|
|
if (contentFile.exists() && defaultContent.exists() &&
|
2023-04-06 13:44:01 +00:00
|
|
|
contentFile.lastModifiedTime < defaultContent.lastModifiedTime) {
|
2023-04-06 13:16:05 +00:00
|
|
|
log("Removing outdated userContent.css from profile");
|
2021-12-04 15:36:56 +00:00
|
|
|
contentFile.remove(false);
|
2021-05-15 18:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy userContent.css to <profile>/chrome/
|
|
|
|
if (!contentFile.exists()) {
|
2023-04-06 13:16:05 +00:00
|
|
|
log("Copying userContent.css from /etc/mobile-config-firefox to profile");
|
2021-12-04 15:36:56 +00:00
|
|
|
defaultContent.copyTo(chromeDir, "userContent.css");
|
2022-02-17 16:17:13 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart Firefox immediately if one of the files got updated
|
2023-04-06 14:48:55 +00:00
|
|
|
if (updated == true)
|
|
|
|
trigger_firefox_restart();
|
|
|
|
else
|
|
|
|
set_default_prefs();
|
2023-04-06 13:16:05 +00:00
|
|
|
|
|
|
|
log("Done");
|
|
|
|
logFileStream.close();
|