Created
January 7, 2020 19:39
-
-
Save obahareth/aad61b3a744201ebd0f087238e4794da to your computer and use it in GitHub Desktop.
A Go script to install Notion RTL (on macOS only for now) for the desktop app. Sadly Notion recreates the HTML file this script modifies on whenever the app is relaunched, so the changes need to keep being reapplied.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/user" | |
"path/filepath" | |
"runtime" | |
"strings" | |
) | |
var rtlScriptTag string = "<script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/gh/obahareth/notion-rtl@master/index.js\"></script>" | |
func getHomeDirectory() string { | |
usr, err := user.Current() | |
if err != nil { | |
panic(err) | |
} | |
return usr.HomeDir | |
} | |
func getNotionAssetCacheDirectory() (string, error) { | |
homeDir := getHomeDirectory() | |
if strings.Contains(runtime.GOOS, "darwin") { | |
return fmt.Sprintf("%s/Library/Application Support/Notion/assetCache/", homeDir), nil | |
} | |
// TODO: Handle Windows | |
return "", errors.New("Unable to find Notion's assets directory") | |
} | |
// Get all index-*.html files for all versions inside the asset cache directory | |
func getAllIndexHTMLFiles() []string { | |
assetCacheDirectory, err := getNotionAssetCacheDirectory() | |
if err != nil { | |
panic(err) | |
} | |
globPattern := fmt.Sprintf("%s**%cindex*.html", assetCacheDirectory, os.PathSeparator) | |
files, err := filepath.Glob(globPattern) | |
if err != nil { | |
panic(err) | |
} | |
return files | |
} | |
func isRTLScriptAlreadyIncluded(fileContents string) bool { | |
return strings.Contains(fileContents, rtlScriptTag) | |
} | |
func includeRTLScriptInHTMLFile(path string) bool { | |
fileContents, err := ioutil.ReadFile(path) | |
if err != nil { | |
panic(err) | |
} | |
if isRTLScriptAlreadyIncluded(string(fileContents)) { | |
return true | |
} | |
closingBodyTag := "</body>" | |
closingBodyTagWithRTLScriptTag := fmt.Sprintf("%s%s", rtlScriptTag, closingBodyTag) | |
newFileContents := strings.Replace(string(fileContents), closingBodyTag, closingBodyTagWithRTLScriptTag, -1) | |
err = ioutil.WriteFile(path, []byte(newFileContents), 0) | |
if err != nil { | |
panic(err) | |
} | |
return true | |
} | |
func includeRTLScriptTagInAllHTMLFiles() bool { | |
files := getAllIndexHTMLFiles() | |
if files == nil { | |
return false | |
} | |
for _, file := range files { | |
if !includeRTLScriptInHTMLFile(file) { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
fmt.Print("Including script in Notion's HTML files...\n\n") | |
if includeRTLScriptTagInAllHTMLFiles() { | |
fmt.Println("✅ Done! Please reload or restart Notion") | |
} else { | |
fmt.Println("❌ Unable to include scripts, please open an issue at https://github.com/obahareth/notion-rtl/issues") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment