This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
forked from antonio-vr/slack-black-theme
-
Notifications
You must be signed in to change notification settings - Fork 59
/
slackBlack.ps1
172 lines (144 loc) · 6 KB
/
slackBlack.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Created by: Anthony Northrup
# Based on: https://github.com/tarantulae/slack-black-theme-4.0PS
# Originally linked: https://github.com/widget-/slack-black-theme/issues/98#issuecomment-512283114
# Extended by: Nockiro
# Modify the interjectCode.js for the theme
# Current snippet from: https://github.com/Nockiro/slack-black-theme/blob/master/interjectCode.js
# Required for MessageBox
Add-Type -AssemblyName System.Windows.Forms
# Ensure Slack version 4 or later is installed
$SlackRoot = $env:LOCALAPPDATA + "\slack"
if (!(Test-Path $env:LOCALAPPDATA\slack\app-4*))
{
if (!(Test-Path $env:LOCALAPPDATA\slack\app-3*))
{
if (!(Test-Path $env:LOCALAPPDATA\slack\app-2*))
{
[System.Windows.MessageBox]::Show('Slack version 2-4 not installed','Error: Exiting...')
exit
} else { $Slack_Major = 2 }
} else { $Slack_Major = 3 }
} else { $Slack_Major = 4 }
$AppVersion = Get-ChildItem -Directory -Path $SlackRoot -Filter "app-$($Slack_Major)*" | Sort-Object LastAccessTime -Descending | Select-Object -First 1 -ExpandProperty Name
$SlackResources = $SlackRoot + "\" + $AppVersion + "\resources"
# Locate 7-Zip
$7zipRoot = Get-ItemProperty HKLM:\Software\7-Zip | Select-Object -ExpandProperty Path
if (!$7zipRoot)
{
$7zipRoot = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip | Select-Object -ExpandProperty InstallLocation
}
if (!$7zipRoot -or !(Test-Path "$7zipRoot\7z.exe"))
{
[System.Windows.Forms.MessageBox]::Show("7-Zip not detected", "Error: Exiting...");
exit
}
# Ensure the ASAR add-in is installed
if (!(Test-Path "$7zipRoot\Formats\Asar*"))
{
[System.Windows.Forms.MessageBox]::Show("ASAR add-in must be installed. Download from: http://www.tc4shell.com/en/7zip/asar/", "Error: Exiting...")
exit
}
# Stop Slack
Get-Process slack -ErrorAction SilentlyContinue | Stop-Process -PassThru
# Already have a backup? Might want to restore
$oldLocation = Get-Location
if ((Test-Path "$SlackResources\app.asar.backup") -or (Test-Path "$SlackResources\app.asar.unpacked\src\static\index.js.backup") -or (Test-Path "$SlackResources\app.asar.unpacked\src\static\ssb-interop.backup"))
{
$result = [System.Windows.Forms.MessageBox]::Show("A backup of the app already exists, do you want to remove the custom theme and restore to the previous version?", "Remove custom theme?", [System.Windows.Forms.MessageBoxButtons]::YesNoCancel, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($result -eq [System.Windows.Forms.DialogResult]::Yes)
{
if ($Slack_Major -eq 4) {
echo "Restoring for Slack 4.."
Move-Item -Force $SlackResources\app.asar.backup $SlackResources\app.asar
} elseif ($Slack_Major -gt 1) {
echo "Restoring for Slack 2+.."
Move-Item -Force $SlackResources\app.asar.unpacked\src\static\index.js.backup $SlackResources\app.asar.unpacked\src\static\index.js
if ($Slack_Major -gt 2) {
echo "Restoring for Slack 3.."
Move-Item -Force $SlackResources\app.asar.unpacked\src\static\ssb-interop.js.backup $SlackResources\app.asar.unpacked\src\static\ssb-interop.js
}
}
# Start slack again
Set-Location $SlackRoot
.\slack.exe
Set-Location $oldLocation
# Prevent further execution
exit
}
elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{
exit
}
}
# Get interject content from file
$customThemeJS = Get-Content interjectCode.js
# Copy the archive to a temp folder
$tempDir = "C:\temp\SlackBlackTheme_Temp"
if (!(Test-Path $tempDir))
{
New-Item -Path $tempDir -ItemType Directory
}
Set-Location $tempDir
if ($Slack_Major -eq 4) {
Copy-Item $SlackResources\app.asar .
# Extract ssb-interop
& $7zipRoot\7z.exe e app.asar -odist dist\ssb-interop.bundle.js -y
# Append the custom JS code as instructed: https://github.com/Nockiro/slack-black-theme
Add-Content dist\ssb-interop.bundle.js -Value $customThemeJS
# Backup already exists?
$updateFiles = $TRUE
if (Test-Path "$SlackResources\app.asar.backup")
{
$updateFiles = $FALSE
$result = [System.Windows.Forms.MessageBox]::Show("A backup of the app already exists, do you want to overwrite?", "Warning: Backup exists", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Warning)
if ($result -eq [System.Windows.Forms.DialogResult]::Yes)
{
$updateFiles = $TRUE
}
}
# Actually update the files
if ($updateFiles)
{
# Update the modified files in the archive
& $7zipRoot\7z.exe u app.asar dist\ssb-interop.bundle.js
# Backup the old archive
Copy-Item $SlackResources\app.asar $SlackResources\app.asar.backup
# Copy the new archive
Copy-Item app.asar $SlackResources
}
} else {
# Backup already exists?
$updateFiles = $TRUE
if ((Test-Path "$SlackResources\app.asar.unpacked\src\static\index.js.backup") -or (Test-Path "$SlackResources\app.asar.unpacked\src\static\ssb-interop.backup"))
{
$updateFiles = $FALSE
$result = [System.Windows.Forms.MessageBox]::Show("A backup of the app already exists, do you want to overwrite?", "Warning: Backup exists", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Warning)
if ($result -eq [System.Windows.Forms.DialogResult]::Yes)
{
$updateFiles = $TRUE
}
}
Copy-Item $SlackResources\app.asar.unpacked\src\static\index.js .
Copy-Item $SlackResources\app.asar.unpacked\src\static\ssb-interop.js .
if ($updateFiles) {
if ($Slack_Major -gt 1) {
echo "Adding code for slack 2+"
Add-Content $SlackResources\app.asar.unpacked\src\static\index.js -Value $customThemeJS
# Backup old index file
Copy-Item .\index.js $SlackResources\app.asar.unpacked\src\static\index.js.backup
if ($Slack_Major -gt 2) {
echo "Adding code for slack 3"
Add-Content $SlackResources\app.asar.unpacked\src\static\ssb-interop.js -Value $customThemeJS
#Backup old ssb-interop file
Copy-Item .\ssb-interop.js $SlackResources\app.asar.unpacked\src\static\ssb-interop.js.backup
}
}
}
}
# Remove the temp files
Set-Location $oldLocation
Remove-Item -Recurse -Path $tempDir
# Start slack again
Set-Location $SlackRoot
.\slack.exe
Set-Location $oldLocation