Skip to content

Instantly share code, notes, and snippets.

@VirenMohindra
Forked from lacostenycoder/README.md
Created October 2, 2018 02:41
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 VirenMohindra/b6df8755326893483038f78e97732f2d to your computer and use it in GitHub Desktop.
Save VirenMohindra/b6df8755326893483038f78e97732f2d to your computer and use it in GitHub Desktop.
Ruby script to toggle night-mode hack on Slack Desktop app - Mac only, maybe linux

- IMPORTANT

For security, since this script injects CSS via AJAX, first fork the main slack night mode repo. The reason is explained here

Installation

  • save this script wherever you keep your ruby scripts for example ~/lacostenycoder/scripts/ruby/
  • change the URL in the ruby script to use YOUR repo. The rawgit.com file is created when you fork the repo.
  • For non-MacOS, find where the slack files are and replace line 17 to point to correct path, thanks simbalinux so you would change line 17 for ubuntu like this
    • @file_target = '/usr/lib/slack/resources/app.asar.unpacked/src/static/ssb-interop.js'
  • make script executable sudo chmod +x nightslack
  • symlink to a PATH load for example: ln -s /Users/lacostenycoder/dev/ruby/nightslack.rb /usr/local/bin/nightslack
  • Or if you prefer just drop the .rb from the filename and move it directly to /usr/local/bin/nightslack

How to use this

From a terminal simply type nightslack to start desktop app in night-mode.

To toggle back to day mode, simpley run nightslack -d to pass the day mode option. This will start or restart a running slack desktop in the desired mode!

Keeping your forked repo up to date

  • To update styles, sync your forked repo with upstream but be sure to inspect the css to insure it doesn't contain js code injeciton.
#!/usr/bin/env ruby
slack_pid = `ps -axcf -S -O%cpu | grep Slack | awk '{print $2}' | head -n 1`.strip
unless slack_pid.empty?
`kill -9 #{slack_pid}`
end
js_code = <<-JS
document.addEventListener('DOMContentLoaded', function() {
$.ajax({
url: 'https://rawgit.com/lacostenyc/slack-night-mode/master/css/raw/black.css',
success: function(css) {
$("<style></style>").appendTo('head').html(css);
}
});
});
JS
@file_target = '/Applications/Slack.app/Contents/Resources/app.asar.unpacked/src/static/ssb-interop.js'
`cp #{@file_target} #{@file_target}.bak`
@current_file = File.read(@file_target)
@normal_mode = @day_mode_set ? @current_file : @current_file.split(js_code).first
@night_mode = @normal_mode + js_code
use_day_mode = ARGV[0] == '-d'
def set_mode(mode)
if mode == 'd'
File.open(@file_target, 'w'){|f| f.puts @normal_mode}
puts "Slack Normal (day) mode has been set!"
else
File.open(@file_target, 'w'){|f| f.puts @night_mode}
puts "Slack Normal night mode has been set!"
end
end
use_day_mode ? set_mode('d') : set_mode('n')
`open -a "Slack"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment