├── .gitignore ├── Makefile ├── package.json ├── readme.md ├── reloader.coffee ├── reloader.js └── test ├── index.html └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | @./node_modules/.bin/coffee -c reloader.coffee 3 | test: 4 | open -a "Safari" test/index.html 5 | open -a "Google Chrome" test/index.html 6 | sleep 3 7 | node test/test.js 8 | 9 | .PHONY: build test 10 | 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "macreloader", 3 | "version": "1.0.3", 4 | "description": "Reload browser with applescript", 5 | "main": "reloader.js", 6 | "scripts": { 7 | "test": "make test" 8 | }, 9 | "author": "Koen Bok", 10 | "license": "MIT", 11 | "dependencies": { 12 | "run-applescript": "^1.0.1" 13 | }, 14 | "devDependencies": { 15 | "coffee-script": "^1.9.0" 16 | }, 17 | "os": [ 18 | "darwin" 19 | ], 20 | "directories": { 21 | "test": "test" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/koenbok/MacReloader.git" 26 | }, 27 | "keywords": [ 28 | "browser", 29 | "reload" 30 | ], 31 | "bugs": { 32 | "url": "https://github.com/koenbok/MacReloader/issues" 33 | }, 34 | "homepage": "https://github.com/koenbok/MacReloader" 35 | } 36 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | #### Browser Reloader 2 | 3 | This script lets you reload any open url in Safari and Chrome (Mac only). It works via AppleScript and is very hacky but reliable. 4 | 5 | It is a pretty great alternative to browsersync (no server required) and livereload (polling). 6 | 7 | #### Usage 8 | 9 | ``` 10 | reloader = require("macreloader") 11 | reloader.reload("file://home/koen/test.html") 12 | ``` 13 | 14 | #### Testing 15 | 16 | - run `make test` 17 | 18 | Your browser(s) should open a test index.html and refresh after three seconds. 19 | 20 | #### Building 21 | 22 | - run `make build` -------------------------------------------------------------------------------- /reloader.coffee: -------------------------------------------------------------------------------- 1 | run = require("run-applescript") 2 | 3 | serializeArray = (arr) -> 4 | # ["a", "b"] -> {"a", "b"} 5 | return "{}" if not arr.length 6 | return "{\"#{arr.join("\", \"")}\"}" 7 | 8 | chromeScript = (urls, js) -> 9 | """ 10 | set hostLists to #{serializeArray(urls)} 11 | tell application "Google Chrome" 12 | set windowsList to windows as list 13 | repeat with currWindow in windowsList 14 | set tabsList to currWindow's tabs as list 15 | repeat with currTab in tabsList 16 | repeat with currentHost in hostLists 17 | if currentHost is in currTab's URL then execute currTab javascript "#{js}" 18 | end repeat 19 | end repeat 20 | end repeat 21 | end tell 22 | """ 23 | 24 | safariScript = (urls, js) -> 25 | """ 26 | set hostLists to #{serializeArray(urls)} 27 | tell application "Safari" 28 | if (count of windows) is greater than 0 then 29 | set windowsList to windows as list 30 | repeat with currWindow in windowsList 31 | set tabsList to currWindow's tabs as list 32 | repeat with currTab in tabsList 33 | repeat with currentHost in hostLists 34 | if currentHost is in currTab's URL then 35 | tell currTab to do JavaScript "#{js}" 36 | end if 37 | end repeat 38 | end repeat 39 | end repeat 40 | end if 41 | end tell 42 | """ 43 | 44 | reloadScript = "location.reload();" 45 | 46 | reloadChrome = (urls, js=reloadScript) -> 47 | run chromeScript(urls, js), (err, result) -> 48 | # throw err if err 49 | 50 | reloadSafari = (urls, js=reloadScript) -> 51 | run safariScript(urls, js), (err, result) -> 52 | # throw err if err 53 | 54 | exports.reload = (urls) -> 55 | urls = [urls] unless Array.isArray(urls) 56 | reloadChrome(urls) 57 | reloadSafari(urls) 58 | 59 | -------------------------------------------------------------------------------- /reloader.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.9.0 2 | (function() { 3 | var chromeScript, reloadChrome, reloadSafari, reloadScript, run, safariScript, serializeArray, _; 4 | 5 | _ = require("lodash"); 6 | 7 | run = require("run-applescript"); 8 | 9 | serializeArray = function(arr) { 10 | if (!arr.length) { 11 | return "{}"; 12 | } 13 | return "{\"" + (arr.join("\", \"")) + "\"}"; 14 | }; 15 | 16 | chromeScript = function(urls, js) { 17 | return "set hostLists to " + (serializeArray(urls)) + "\ntell application \"Google Chrome\"\n set windowsList to windows as list\n repeat with currWindow in windowsList\n set tabsList to currWindow's tabs as list\n repeat with currTab in tabsList\n repeat with currentHost in hostLists\n if currentHost is in currTab's URL then execute currTab javascript \"" + js + "\"\n end repeat\n end repeat\n end repeat\nend tell"; 18 | }; 19 | 20 | safariScript = function(urls, js) { 21 | return "set hostLists to " + (serializeArray(urls)) + "\ntell application \"Safari\"\n if (count of windows) is greater than 0 then\n set windowsList to windows as list\n repeat with currWindow in windowsList\n set tabsList to currWindow's tabs as list\n repeat with currTab in tabsList\n repeat with currentHost in hostLists\n if currentHost is in currTab's URL then\n tell currTab to do JavaScript \"" + js + "\"\n end if\n end repeat \n end repeat\n end repeat\n end if\nend tell"; 22 | }; 23 | 24 | reloadScript = "location.reload();"; 25 | 26 | reloadChrome = function(urls, js) { 27 | if (js == null) { 28 | js = reloadScript; 29 | } 30 | return run(chromeScript(urls, js), function(err, result) {}); 31 | }; 32 | 33 | reloadSafari = function(urls, js) { 34 | if (js == null) { 35 | js = reloadScript; 36 | } 37 | return run(safariScript(urls, js), function(err, result) {}); 38 | }; 39 | 40 | exports.reload = function(urls) { 41 | if (!_.isArray(urls)) { 42 | urls = [urls]; 43 | } 44 | reloadChrome(urls); 45 | return reloadSafari(urls); 46 | }; 47 | 48 | }).call(this); 49 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Testing a reload... 5 | 6 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | path = require("path"); 2 | reloader = require("../reloader.js"); 3 | 4 | indexPath = "file://" + path.join(__dirname, "index.html") 5 | 6 | console.log("Reloading", indexPath) 7 | 8 | reloader.reload(indexPath) 9 | --------------------------------------------------------------------------------