Skip to content

Commit

Permalink
a more clever fix to #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalik Gordon committed Dec 9, 2020
1 parent a90c3ff commit e750db2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Here's [a blog post explaining it](https://60devs.com/hot-reloading-for-chrome-e
2. Put the following into your `manifest.json` file:

```json
"background": { "scripts": ["hot-reload.js"] },
"permissions": ["http://*/*", "https://*/*"],
"background": { "scripts": ["hot-reload.js"] }
```

Also, you can simply clone this repository and use it as a boilerplate for your extension.
Expand Down
25 changes: 6 additions & 19 deletions hot-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,23 @@ const timestampForFilesInDirectory = dir =>
filesInDirectory (dir).then (files =>
files.map (f => f.name + f.lastModifiedDate).join ())

const reload = () => {
chrome.permissions.contains ({
origins: ["http://*/*", "https://*/*"]
}, granted => {
if (granted) {
chrome.tabs.query ({ active: true, lastFocusedWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5
if (tabs[0]) {
chrome.tabs.executeScript (tabs[0].id, { code: 'setTimeout(() => { location.reload() }, 300)' }, () => {})
chrome.runtime.reload ()
}
})
} else {
alert ('Unable to reload the active tab — please add "http://*/*" and "https://*/*" permissions to manifest.json!')
chrome.runtime.reload ()
}
})
}

const watchChanges = (dir, lastTimestamp) => {
timestampForFilesInDirectory (dir).then (timestamp => {
if (!lastTimestamp || (lastTimestamp === timestamp)) {
setTimeout (() => watchChanges (dir, timestamp), 1000) // retry after 1s
} else {
reload ()
chrome.runtime.reload ()
}
})
}

chrome.management.getSelf (self => {
if (self.installType === 'development') {
chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
chrome.tabs.query ({ active: true, lastFocusedWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5
if (tabs[0]) {
chrome.tabs.reload (tabs[0].id)
}
})
}
})
2 changes: 0 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"description": "An example extension which demonstrates hot-reloading",
"version": "1.1337.0",

"permissions": ["http://*/*", "https://*/*"],

"background": {
"scripts": ["hot-reload.js"]
}
Expand Down

0 comments on commit e750db2

Please sign in to comment.