Skip to content

Instantly share code, notes, and snippets.

@jespertheend
Last active March 20, 2023 09:00
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 jespertheend/9b64ce3b4d6eb433540304c228c4fee5 to your computer and use it in GitHub Desktop.
Save jespertheend/9b64ce3b4d6eb433540304c228c4fee5 to your computer and use it in GitHub Desktop.
Ubuntu Chromium Canary
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-net
/**
* @fileoverview Downloads the latest Linux Chromium development build from Googles servers and then runs it.
* When a build has already been downloaded, it first runs that version and then downloads a new version in the background.
* Only works on Linux. If you are using Windows or macOS you can get Nightly builds from https://www.google.com/chrome/canary/
*
* ## Usage
*
* ```
* ./canary.js -dg
* ```
*
* Use `-d` to run in dark mode.
* Use `-g` to enable WebGPU support.
*/
import parseArgs from "https://deno.land/x/deno_minimist@v1.0.2/mod.ts";
import * as fs from "https://raw.githubusercontent.com/denoland/deno_std/7032f6856193b84f2046b6762698939754469c02/fs/mod.ts";
import { decompress } from "https://deno.land/x/zip@v1.2.5/mod.ts";
import ProgressBar from "https://deno.land/x/progressbar@v0.2.0/progressbar.ts";
import { percentageWidget } from "https://deno.land/x/progressbar@v0.2.0/widgets.ts";
import { setCwd } from "https://deno.land/x/chdir_anywhere/mod.js";
setCwd();
async function tryRemove(path) {
try {
await Deno.remove(path, {recursive: true})
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e;
}
}
async function downloadUpdate() {
const latestResponse = await fetch("https://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64/LAST_CHANGE");
if (!latestResponse.ok) throw new Error("Couldn't get latest version");
const latestVersionStr = await latestResponse.text();
let currentStr = "";
try {
currentStr = await Deno.readTextFile("current.txt");
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e;
}
if (currentStr == latestVersionStr) return {
update: async () => {}
};
console.log("Updating Chromium");
const latest = await fetch(`https://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64/${latestVersionStr}/chrome-linux.zip`);
if (!latest.ok) throw new Error("Couldn't get latest version");
const contentLength = parseInt(latest.headers.get("content-length"), 10);
const progressBar = new ProgressBar({
total: contentLength,
widgets: [
percentageWidget,
],
});
const reader = latest.body.getReader();
let receivedBytes = 0;
const chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
receivedBytes += value.length;
chunks.push(value);
progressBar.update(receivedBytes);
}
const buffer = new Uint8Array(receivedBytes);
let position = 0;
for(const chunk of chunks) {
buffer.set(chunk, position);
position += chunk.length;
}
await fs.ensureDir("tmp");
await Deno.writeFile("tmp/latest.zip", buffer);
await tryRemove("tmp/chrome-linux");
await decompress("tmp/latest.zip", "tmp");
async function update() {
await tryRemove("chrome-linux");
await Deno.rename("tmp/chrome-linux", "chrome-linux");
await Deno.writeTextFile("current.txt", latestVersionStr);
}
return {update}
}
if (!await fs.exists("chrome-linux")) {
const {update} = await downloadUpdate();
await update();
}
const argv = parseArgs(Deno.args, {
default: {
d: false,
g: false,
},
});
const cmd = ["./chrome-linux/chrome"];
if (argv.d) {
cmd.push("--force-dark-mode","--enable-features=WebUIDarkMode");
}
if (argv.g) {
cmd.push("--enable-features=ReduceOpsTaskSplitting,Vulkan,VulkanFromANGLE,DefaultANGLEVulkan", "--enable-unsafe-webgpu", "--use-angle=swiftshader")
}
const process = Deno.run({
cmd,
})
const {update} = await downloadUpdate();
await process.status();
await update();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment