Skip to content

Commit

Permalink
Update injection procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
fsavje committed Jun 14, 2018
1 parent d385db5 commit 07f2fe2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 66 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Fredrik Sävje
Copyright (c) 2017-2018 Fredrik Sävje

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
@@ -1,3 +1,9 @@
# math-with-slack 0.2.4

* Minor fixes.
* Uses MathJax v2.7.4.


# math-with-slack 0.2.3

* Minor fixes.
Expand Down
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -22,22 +22,25 @@ Download and run your platform's script. Restart the Slack client. You're done!
Run the following in a terminal:

```shell
curl -OL https://github.com/fsavje/math-with-slack/releases/download/v0.2.3/math-with-slack.sh
curl -OL https://github.com/fsavje/math-with-slack/releases/download/v0.2.4/math-with-slack.sh
sudo bash math-with-slack.sh
```


### Windows

[Download the script](https://github.com/fsavje/math-with-slack/releases/download/v0.2.3/math-with-slack.bat) and double-click to run. Alternatively, run it in the command prompt with:
[Download the script](https://github.com/fsavje/math-with-slack/releases/download/v0.2.4/math-with-slack.bat) and double-click to run. Alternatively, run it in the command prompt with:

```shell
math-with-slack.bat
```

(Windows will probably give you a security warning when running the script since it's downloaded from Internet.)
(Windows will probably give you a security warning since the script is downloaded from Internet.)

The script doesn't work with the Windows Store version of the Slack app. The Windows Store version is write-protected and can't be injected with the MathJax code. The version downloaded from [Slack's website](https://slack.com/downloads/windows) should, however, work.

### Package and software managers

The script needs write permissions in the Slack directory in order to inject the MathJax code. Some package and software managers write protect their directories, and `math-with-slack` cannot be installed if Slack is installed through such a manager. This is the case for both the Windows Store and Snap versions of Slack. You should use the version downloaded from [Slack's website](https://slack.com/downloads/windows) if you want to use `math-with-slack`. The script should, however, work with most package managers if you manage to grant the script write permission.


### Uninstall
Expand Down Expand Up @@ -99,6 +102,7 @@ Yes, please. Just add an [issue](https://github.com/fsavje/math-with-slack/issue
* [NKudryavka](https://github.com/NKudryavka)
* [peroxyacyl](https://github.com/peroxyacyl)
* [Spenhouet](https://github.com/Spenhouet)
* [Xyene](https://github.com/Xyene)


**Inspiration**
Expand Down
126 changes: 66 additions & 60 deletions math-with-slack.bat
Expand Up @@ -11,14 +11,14 @@
::
:: https://github.com/fsavje/math-with-slack
::
:: MIT License, Copyright 2018 Fredrik Savje
:: MIT License, Copyright 2017-2018 Fredrik Savje
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: Constants

SET "MWS_VERSION=v0.2.3"
SET "MWS_VERSION=v0.2.4"


:: User input
Expand Down Expand Up @@ -47,6 +47,12 @@ IF "%SLACK_DIR%" == "" (
)


:: Files

SET "SLACK_MATHJAX_SCRIPT=%SLACK_DIR%/math-with-slack.js"
SET "SLACK_SSB_INTEROP=%SLACK_DIR%/ssb-interop.js"


:: Check so installation exists

IF "%SLACK_DIR%" == "" (
Expand All @@ -59,8 +65,8 @@ IF NOT EXIST "%SLACK_DIR%" (
PAUSE & EXIT /B 1
)

IF NOT EXIST "%SLACK_DIR%\ssb-interop.js" (
ECHO Cannot find Slack file: %SLACK_DIR%\ssb-interop.js
IF NOT EXIST "%SLACK_SSB_INTEROP%" (
ECHO Cannot find Slack file: %SLACK_SSB_INTEROP%
PAUSE & EXIT /B 1
)

Expand All @@ -70,15 +76,26 @@ ECHO Using Slack installation at: %SLACK_DIR%

:: Remove previous version

IF EXIST "%SLACK_DIR%\math-with-slack.js" (
DEL "%SLACK_DIR%\math-with-slack.js"
IF EXIST "%SLACK_MATHJAX_SCRIPT%" (
DEL "%SLACK_MATHJAX_SCRIPT%"
)


:: Restore previous injections

CALL :restore_file "%SLACK_DIR%\ssb-interop.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )
FINDSTR /R /C:"math-with-slack" "%SLACK_SSB_INTEROP%" >NUL
IF %ERRORLEVEL% EQU 0 (
IF EXIST "%SLACK_SSB_INTEROP%.mwsbak" (
MOVE /Y "%SLACK_SSB_INTEROP%.mwsbak" "%SLACK_SSB_INTEROP%" >NUL
) ELSE (
ECHO Cannot restore from backup. Missing file: %SLACK_SSB_INTEROP%.mwsbak
PAUSE & EXIT /B 1
)
) ELSE (
IF EXIST "%SLACK_SSB_INTEROP%.mwsbak" (
DEL "%SLACK_SSB_INTEROP%.mwsbak"
)
)


:: Are we uninstalling?
Expand All @@ -91,7 +108,7 @@ IF "%UNINSTALL%" == "-u" (

:: Write main script

>"%SLACK_DIR%\math-with-slack.js" (
>"%SLACK_MATHJAX_SCRIPT%" (
ECHO.// math-with-slack %MWS_VERSION%
ECHO.// https://github.com/fsavje/math-with-slack
ECHO.
Expand All @@ -116,75 +133,64 @@ IF "%UNINSTALL%" == "-u" (
ECHO. }
ECHO. }^);
ECHO. `;
ECHO.
ECHO. var mathjax_observer = document.createElement('script'^);
ECHO. mathjax_observer.type = 'text/x-mathjax-config';
ECHO. mathjax_observer.text = `
ECHO. var target = document.querySelector('#messages_container'^);
ECHO. var options = { attributes: false, childList: true, characterData: true, subtree: true };
ECHO. var observer = new MutationObserver(function (r, o^) { MathJax.Hub.Queue(['Typeset', MathJax.Hub]^); }^);
ECHO. observer.observe(target, options^);
ECHO. `;
ECHO.
ECHO. var mathjax_script = document.createElement('script'^);
ECHO. mathjax_script.type = 'text/javascript';
ECHO. mathjax_script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js';
ECHO. mathjax_script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js';
ECHO.
ECHO. document.head.appendChild(mathjax_config^);
ECHO. document.head.appendChild(mathjax_observer^);
ECHO. document.head.appendChild(mathjax_script^);
ECHO.
ECHO. var target = document.querySelector('#messages_container'^);
ECHO. var options = { attributes: false, childList: true, characterData: true, subtree: true };
ECHO. var observer = new MutationObserver(function (r, o^) { MathJax.Hub.Queue(['Typeset', MathJax.Hub]^); }^);
ECHO. observer.observe(target, options^);
ECHO.}^);
)


:: Inject code loader
:: Check so not already injected

CALL :inject_loader "%SLACK_DIR%\ssb-interop.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )
FINDSTR /R /C:"math-with-slack" "%SLACK_SSB_INTEROP%" >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO File already injected: %SLACK_SSB_INTEROP%
PAUSE & EXIT /B 1
)


:: We're done
:: Make backup

ECHO math-with-slack has been installed. Please restart the Slack client.
PAUSE & EXIT /B 0
IF NOT EXIST "%SLACK_SSB_INTEROP%.mwsbak" (
MOVE /Y "%SLACK_SSB_INTEROP%" "%SLACK_SSB_INTEROP%.mwsbak" >NUL
) ELSE (
ECHO Backup already exists: %SLACK_SSB_INTEROP%.mwsbak
PAUSE & EXIT /B 1
)


:: Functions
:: Inject loader code

:restore_file
FINDSTR /R /C:"math-with-slack" "%~1" >NUL
IF %ERRORLEVEL% EQU 0 (
IF EXIST "%~1.mwsbak" (
MOVE /Y "%~1.mwsbak" "%~1" >NUL
FOR /F "delims=" %%L IN (%SLACK_SSB_INTEROP%.mwsbak) DO (
IF "%%L" == " init(resourcePath, mainModule, !isDevMode);" (
>>"%SLACK_SSB_INTEROP%" (
ECHO. // ** math-with-slack %MWS_VERSION% ** https://github.com/fsavje/math-with-slack
ECHO. var mwsp = path.join(__dirname, 'math-with-slack.js'^).replace('app.asar', 'app.asar.unpacked'^);
ECHO. require('fs'^).readFile(mwsp, 'utf8', (e, r^) =^> { if (e^) { throw e; } else { eval(r^); } }^);
ECHO.
ECHO. init(resourcePath, mainModule, !isDevMode^);
)
) ELSE (
ECHO Cannot restore from backup. Missing file: %~1.mwsbak
EXIT /B 1
)
) ELSE (
IF EXIST "%~1.mwsbak" (
DEL "%~1.mwsbak"
>>"%SLACK_SSB_INTEROP%" ECHO.%%L
)
)
EXIT /B 0
:: end restore_file


:inject_loader
:: Check so not already injected
FINDSTR /R /C:"math-with-slack" "%~1" >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO File already injected: %~1
EXIT /B 1
)

:: Make backup
IF NOT EXIST "%~1.mwsbak" (
COPY "%~1" "%~1.mwsbak" >NUL
) ELSE (
ECHO Backup already exists: %~1.mwsbak
EXIT /B 1
)

:: Inject loader code
>>"%~1" (
ECHO.
ECHO.// ** math-with-slack %MWS_VERSION% ** https://github.com/fsavje/math-with-slack
ECHO.var mwsp = path.join(__dirname, 'math-with-slack.js'^).replace('app.asar', 'app.asar.unpacked'^);
ECHO.require('fs'^).readFile(mwsp, 'utf8', (e, r^) =^> { if (e^) { throw e; } else { eval(r^); } }^);
)
:: We're done

EXIT /B 0
:: end inject_loader
ECHO math-with-slack has been installed. Please restart the Slack client.
PAUSE & EXIT /B 0
1 change: 0 additions & 1 deletion math-with-slack.sh
Expand Up @@ -198,4 +198,3 @@ EOF
## We're done

echo "$(tput setaf 64)math-with-slack has been installed. Please restart the Slack client.$(tput sgr0)"

0 comments on commit 07f2fe2

Please sign in to comment.