Skip to content

Commit

Permalink
Add version 0.2
Browse files Browse the repository at this point in the history
This fixes #8
  • Loading branch information
fsavje committed Jun 7, 2017
1 parent 92c76ea commit 385c152
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 262 deletions.
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# math-with-slack 0.2

* Loads without delay.
* Works when changing teams.
* Uses MathJax v2.7.1.


# math-with-slack 0.1

* Initial release.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

## How do I install it?

Download your platform's script and run it. After restarting the Slack client, you're all done! (It takes up to 30 seconds for MathJax to load when Slack starts, so be patient.)
Download and run your platform's script. Restart the Slack client. You're all done!


### Mac and Linux

Run the following in a terminal:

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


### Windows

[Download this script](https://github.com/fsavje/math-with-slack/releases/download/r/math_with_slack.bat) and doubleclick to run. Alternatively, run it in the command prompt with:
[Download this script](https://github.com/fsavje/math-with-slack/releases/download/v0.2/math_with_slack.bat) and doubleclick to run. Alternatively, run it in the command prompt with:

```shell
math_with_slack.bat
Expand All @@ -32,23 +32,34 @@ math_with_slack.bat
(You'll most likely get a security warning from Windows since the script was downloaded from Internet.)


### Uninstall

To uninstall, run the script again with the `-u` flag:

```shell
sudo bash math_with_slack.sh -u
```

```shell
math_with_slack.bat -u
```


### Updating Slack

When you update the Slack app, it might overwrite the code injected by the script. If the client stops rendering math after an update, re-run the script as above and it should work again.


### If Slack cannot be found

If you've installed Slack in some exotic place, the script might not find the installation by itself and you need to specify the location of Slack's `index.js` file as a parameter. E.g., like this on Mac and Linux:
If you've installed Slack in some exotic place, the script might not find the installation by itself and you need to specify the location of Slack's `app.asar.unpacked/src/static` folder as a parameter:

```shell
sudo bash math_with_slack.sh /My_Apps/Slack.app/Contents/Resources/app.asar.unpacked/src/static/index.js
sudo bash math_with_slack.sh /My_Apps/Slack.app/Contents/Resources/app.asar.unpacked/src/static
```

and like this on Windows:

```shell
math_with_slack.bat E:\My_Apps\slack\app-2.5.1\resources\app.asar.unpacked\src\static\index.js
math_with_slack.bat E:\My_Apps\slack\app-2.5.1\resources\app.asar.unpacked\src\static
```


Expand Down Expand Up @@ -79,6 +90,6 @@ Yes, please. Just add an [issue](https://github.com/fsavje/math-with-slack/issue
* [peroxyacyl](https://github.com/peroxyacyl)


**References and inspiration**
**Inspiration**

This [comment](https://gist.github.com/DrewML/0acd2e389492e7d9d6be63386d75dd99#gistcomment-1981178) by [jouni](https://github.com/jouni) was extremely helpful. So was this [snippet](https://gist.github.com/etihwnad/bc63ec9b87af586e1435) by [etihwnad](https://github.com/etihwnad).
252 changes: 139 additions & 113 deletions math_with_slack.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
:: Rendered math (MathJax) with Slack's desktop client
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Slack (https://slack.com) does not display rendered math. This script injects
:: MathJax (https://www.mathjax.org) into Slack's desktop client, which allows
:: you to write nice-looking inline- and display-style math using familiar
:: TeX/LaTeX syntax. You can also edit equations after you've posted them.
:: Slack (https://slack.com) does not display rendered math. This script
:: injects MathJax (https://www.mathjax.org) into Slack's desktop client,
:: which allows you to write nice-looking inline- and display-style math
:: using familiar TeX/LaTeX syntax.
::
:: https://github.com/fsavje/math-with-slack
::
Expand All @@ -16,160 +16,186 @@
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: Constants

SET "MWS_VERSION=v0.2"


:: User input

SET "FORCE="
SET "SLACK_INDEX="
SET "UNINSTALL="
SET "SLACK_DIR="

:parse
IF "%~1" == "" GOTO endparse
IF "%~1" == "-f" (
SET FORCE=%~1
IF "%~1" == "-u" (
SET UNINSTALL=%~1
) ELSE (
SET SLACK_INDEX=%~1
SET SLACK_DIR=%~1
)
SHIFT
GOTO parse
:endparse


:: If the user-provided "index.js" is not found, try to find it

IF NOT "%SLACK_INDEX%" == "" IF NOT EXIST "%SLACK_INDEX%" (
IF EXIST "%SLACK_INDEX%AppData\Local\slack\app-2.5.1\resources\app.asar.unpacked\src\static\index.js" (
SET SLACK_INDEX=%SLACK_INDEX%AppData\Local\slack\app-2.5.1\resources\app.asar.unpacked\src\static\index.js
) ELSE IF EXIST "%SLACK_INDEX%\AppData\Local\slack\app-2.5.1\resources\app.asar.unpacked\src\static\index.js" (
SET SLACK_INDEX=%SLACK_INDEX%\AppData\Local\slack\app-2.5.1\resources\app.asar.unpacked\src\static\index.js
) ELSE IF EXIST "%SLACK_INDEX%app-2.5.1\resources\app.asar.unpacked\src\static\index.js" (
SET SLACK_INDEX=%SLACK_INDEX%app-2.5.1\resources\app.asar.unpacked\src\static\index.js
) ELSE IF EXIST "%SLACK_INDEX%\app-2.5.1\resources\app.asar.unpacked\src\static\index.js" (
SET SLACK_INDEX=%SLACK_INDEX%\app-2.5.1\resources\app.asar.unpacked\src\static\index.js
)
)


:: Try to find slack if not provided by user

IF "%SLACK_INDEX%" == "" (
IF "%SLACK_DIR%" == "" (
FOR /F %%t IN ('DIR /B /ON %UserProfile%\AppData\Local\slack\app-?.*.*') DO (
SET SLACK_INDEX=%UserProfile%\AppData\Local\slack\%%t\resources\app.asar.unpacked\src\static\index.js
SET SLACK_DIR=%UserProfile%\AppData\Local\slack\%%t\resources\app.asar.unpacked\src\static
)
)


:: Check so "index.js" exists
:: Check so installation exists

IF "%SLACK_INDEX%" == "" (
ECHO Cannot find Slack's index file.
IF "%SLACK_DIR%" == "" (
ECHO Cannot find Slack installation.
PAUSE & EXIT /B 1
)

IF NOT EXIST "%SLACK_INDEX%" (
ECHO Cannot find Slack's index file: %SLACK_INDEX%
IF NOT EXIST "%SLACK_DIR%" (
ECHO Cannot find Slack installation at: %SLACK_DIR%
PAUSE & EXIT /B 1
)

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

:: Does backup exists? If so, do update

IF EXIST "%SLACK_INDEX%.mwsbak" (
COPY /Y "%SLACK_INDEX%.mwsbak" "%SLACK_INDEX%" >NUL
)
:: Unistall version 0.1
:: (Remove this eventually)

IF EXIST "%SLACK_DIR%\index.js.mwsbak" (
MOVE /Y "%SLACK_DIR%\index.js.mwsbak" "%SLACK_DIR%\index.js" >NUL
)

:: Check so "index.js" is known to work with the script

FOR /F "skip=1 delims=" %%L IN ('CertUtil -hashfile %SLACK_INDEX%') DO SET "INDEX_HASH=%%L" & GOTO breakhashloop
:breakhashloop
:: Remove previous version

IF "%FORCE%" == "" IF NOT "%INDEX_HASH%" == "f07fabb32b109500fb264083b8685a85197df522" (
ECHO Unrecognized index file: %SLACK_INDEX%
ECHO Call with '-f' flag to suppress this check.
PAUSE & EXIT /B 1
IF EXIST "%SLACK_DIR%\math-with-slack.js" (
DEL "%SLACK_DIR%\math-with-slack.js"
)


:: Ensure "index.js" contains "startup();"
:: Restore previous injections

FINDSTR /R /C:"^ startup();" "%SLACK_INDEX%" >NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO Cannot find 'startup(^);' in index file: %SLACK_INDEX%
PAUSE & EXIT /B 1
)
CALL :restore_file "%SLACK_DIR%\ssb-interop.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )

CALL :restore_file "%SLACK_DIR%\ssb-interop-lite.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )

:: Does backup exists? If not, make one

IF NOT EXIST "%SLACK_INDEX%.mwsbak" (
COPY "%SLACK_INDEX%" "%SLACK_INDEX%.mwsbak" >NUL
)
:: Are we uninstalling?

IF "%UNINSTALL%" == "-u" (
ECHO math-with-slack has been uninstalled. Please restart Slack client.
PAUSE & EXIT /B 0
)

:: Write code for MathJax injection

DEL "%SLACK_INDEX%"
IF EXIST "%SLACK_INDEX%" (
ECHO Cannot write to Slack's index file: %SLACK_INDEX%
PAUSE & EXIT /B 1
:: Write main script

>"%SLACK_DIR%\math-with-slack.js" (
ECHO.// math-with-slack %MWS_VERSION%
ECHO.// https://github.com/fsavje/math-with-slack
ECHO.
ECHO.document.addEventListener('DOMContentLoaded', function(^) {
ECHO. var mathjax_config = document.createElement('script'^);
ECHO. mathjax_config.type = 'text/x-mathjax-config';
ECHO. mathjax_config.text = `
ECHO. MathJax.Hub.Config({
ECHO. messageStyle: 'none',
ECHO. extensions: ['tex2jax.js'],
ECHO. jax: ['input/TeX', 'output/HTML-CSS'],
ECHO. tex2jax: {
ECHO. displayMath: [['\$\$', '\$\$']],
ECHO. element: 'msgs_div',
ECHO. ignoreClass: 'ql-editor',
ECHO. inlineMath: [['\$', '\$']],
ECHO. processEscapes: true,
ECHO. skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
ECHO. },
ECHO. TeX: {
ECHO. extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']
ECHO. }
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.1/MathJax.js';
ECHO. document.head.appendChild(mathjax_config^);
ECHO. document.head.appendChild(mathjax_script^);
ECHO.
ECHO. var target = document.querySelector('#msgs_div'^);
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.}^);
)

FOR /F "delims=" %%L IN (%SLACK_INDEX%.mwsbak) DO (
IF "%%L" == " startup();" (
>>"%SLACK_INDEX%" (
ECHO. startup(^);
ECHO.
ECHO. // *** Code injected for MathJax support
ECHO. // See: https://github.com/fsavje/math-with-slack
ECHO.
ECHO. var mathjax_inject_script = `
ECHO. var mathjax_config = document.createElement("script"^);
ECHO. mathjax_config.type = "text/x-mathjax-config";
ECHO. mathjax_config.text = \`
ECHO. MathJax.Hub.Config({
ECHO. messageStyle: "none",
ECHO. extensions: ["tex2jax.js"],
ECHO. jax: ["input/TeX", "output/HTML-CSS"],
ECHO. tex2jax: {
ECHO. skipTags: ["script","noscript","style","textarea","pre","code"],
ECHO. ignoreClass: "ql-editor",
ECHO. inlineMath: [ ['\$','\$'] ],
ECHO. displayMath: [ ['\$\$','\$\$'] ],
ECHO. processEscapes: true
ECHO. },
ECHO. TeX: {
ECHO. extensions: ["AMSmath.js", "AMSsymbols.js", "noErrors.js", "noUndefined.js"]
ECHO. }
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.0/MathJax.js";
ECHO. document.getElementsByTagName("head"^)[0].appendChild(mathjax_config^);
ECHO. document.getElementsByTagName("head"^)[0].appendChild(mathjax_script^);
ECHO.
ECHO. var render = function (records, observer^) {
ECHO. MathJax.Hub.Queue(["Typeset", MathJax.Hub]^);
ECHO. };
ECHO. var target = document.querySelector('#msgs_div'^);
ECHO. var observer = new MutationObserver(render^);
ECHO. var config = { attributes: false, childList: true, characterData: true, subtree: true };
ECHO. observer.observe(target, config^);
ECHO. `;
ECHO.
ECHO. window.webviews = document.querySelectorAll(".TeamView webview"^);
ECHO. setTimeout(function(^) {
ECHO. for(var i = 0; i ^< webviews.length; i++^) {
ECHO. webviews[i].executeJavaScript(mathjax_inject_script^);
ECHO. }
ECHO. }, 20000^);
ECHO.
ECHO. // *** End injected MathJax
ECHO.
)

:: Inject code loader

CALL :inject_loader "%SLACK_DIR%\ssb-interop.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )

CALL :inject_loader "%SLACK_DIR%\ssb-interop-lite.js"
IF %ERRORLEVEL% NEQ 0 ( PAUSE & EXIT /B 1 )


:: We're done

ECHO math-with-slack has been installed. Please restart Slack client.
PAUSE & EXIT /B 0


:: Functions

:restore_file
FINDSTR /R /C:"math-with-slack" "%~1" >NUL
IF %ERRORLEVEL% EQU 0 (
IF EXIST "%~1.mwsbak" (
MOVE /Y "%~1.mwsbak" "%~1" >NUL
) ELSE (
>>"%SLACK_INDEX%" ECHO.%%L
ECHO Cannot restore from backup. Missing file: %~1.mwsbak
EXIT /B 1
)
) ELSE (
IF EXIST "%~1.mwsbak" (
DEL "%~1.mwsbak"
)
)
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^); } }^);
)

ECHO MathJax successfully injected into Slack. Please restart Slack client.
PAUSE
EXIT /B 0
:: end inject_loader
Loading

0 comments on commit 385c152

Please sign in to comment.