Skip to content

Instantly share code, notes, and snippets.

@nlowe
Last active July 28, 2019 05:08
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 nlowe/9da39b1fe88edc607bc343c482afa65a to your computer and use it in GitHub Desktop.
Save nlowe/9da39b1fe88edc607bc343c482afa65a to your computer and use it in GitHub Desktop.
Dark Slack on Windows
#Requires -RunAsAdministrator
# Heavily based off of https://github.com/LanikSJ/slack-dark-mode, which is licensed under the GPLv3 License
Param(
[string] $CSSUrl = "https://raw.githubusercontent.com/LanikSJ/slack-dark-mode/master/dark-theme.css",
[string] $SlackBase = $null,
[switch] $UpdateOnly
)
if (-not (Get-Command -Name "npx" -ErrorAction SilentlyContinue)) {
throw "npx is required to unpack slack"
}
$latestPath = $SlackBase
if ([string]::IsNullOrWhiteSpace($SlackBase)) {
Write-Output "Locating Slack"
$SlackBase = Join-Path -Path $env:LOCALAPPDATA -ChildPath slack
if (-not (Test-Path -Path $SlackBase)) {
Write-Output "Checking for MSI-based installations"
$latestPath = Join-Path -Path $env:ProgramFiles -ChildPath "Slack"
if (-not (Test-Path -Path $latestPath)) {
throw "It doesn't look like slack is installed"
}
Write-Output "Found Slack MSI Install"
} else {
$latestPath = Get-ChildItem -Path $SlackBase -Directory -Filter "app*" | Sort-Object -Descending | Select-Object -First 1 -ExpandProperty FullName
Write-Output "Found Slack $($latestPath.Name) in AppData"
}
} elseif (-not (Test-Path -Path $SlackBase)) {
throw "Could not find $SlackBase"
}
$resources = Join-Path -Path $latestPath -ChildPath "resources"
$themeFile = Join-Path -Path $resources -ChildPath "custom_theme.css"
Write-Output "Stopping Slack"
Get-Process *slack* | Stop-Process -Force
Write-Output "Updating Theme File"
if (-not (Test-Path -Path $themeFile)) {
New-Item -ItemType File -Path $themeFile | Out-Null
}
Write-Output "Fetching Theme from $CSSUrl"
Invoke-WebRequest -UseBasicParsing -Method GET -Uri $CSSUrl | Select-Object -ExpandProperty Content | Set-Content -Path $themeFile
if (-not $UpdateOnly) {
$asar = Join-Path -Path $resources -ChildPath "app.asar"
$unpacked = Join-Path -Path $resources -ChildPath "app.asar.unpacked"
Write-Output "Unpacking asar archive"
&npx asar extract $asar $unpacked
Write-Output "Adding Hook"
$ssbInterop = Join-Path -Path $unpacked -ChildPath "dist" | Join-Path -ChildPath "ssb-interop.bundle.js"
$src = Get-Content -Raw $ssbInterop
if ($src.Contains("// BEGIN CUSTOM THEME")) {
Write-Output "Replacing Old Hook"
$src = $src.Substring(0, $src.IndexOf("// BEGIN CUSTOM THEME"))
}
$src.Trim() + @"
// BEGIN CUSTOM THEME
document.addEventListener('DOMContentLoaded', function() {
const fs = require('fs');
const filePath = "$($themeFile.Replace("\", "/"))";
const tt__customCss = '.menu ul li a:not(.inline_menu_link) {color: #fff !important;}'
fs.readFile(filePath, {
encoding: 'utf-8'
}, function(err, css) {
if (!err) {
`$("<style></style>").appendTo('head').html(css + tt__customCss);
}
});
});
// END CUSTOM THEME
"@ | Set-Content -Path $ssbInterop
Write-Output "Packing asar archive"
&npx asar pack $unpacked $asar
Write-Output "Adding workaround to ensure theme boots"
$localSettingsPath = Join-Path -Path $env:APPDATA -ChildPath "Slack" | Join-Path -ChildPath "local-settings.json"
$localSettings = Get-Content -Raw -Path $localSettingsPath | ConvertFrom-Json
if (-not ($localSettings | Get-Member -Name "bootSonic")) {
$localSettings | Add-Member -MemberType NoteProperty -Name "bootSonic" -Value "never"
} else {
$localSettings.bootSonic = "never"
}
Set-ItemProperty -Path $localSettingsPath -Name IsReadOnly -Value $false
$localSettings | ConvertTo-Json -Compress | Set-Content -Path $localSettingsPath
Set-ItemProperty -Path $localSettingsPath -Name IsReadOnly -Value $true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment