Skip to content

Commit

Permalink
fixes for #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalik Gordon committed Dec 9, 2020
1 parent 640c3d8 commit e7a4b6d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
17 changes: 3 additions & 14 deletions hot-reload.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const filesInDirectory = dir => new Promise (resolve =>

dir.createReader ().readEntries (entries =>

Promise.all (entries.filter (e => e.name[0] !== '.').map (e =>

e.isDirectory
? filesInDirectory (e)
: new Promise (resolve => e.file (resolve))
Expand All @@ -19,7 +16,7 @@ const timestampForFilesInDirectory = dir =>

const reload = () => {
chrome.permissions.contains ({
permissions: ["activeTab"]
origins: ["http://*/*", "https://*/*"]
}, granted => {
if (granted) {
chrome.tabs.query ({ active: true, lastFocusedWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5
Expand All @@ -29,32 +26,24 @@ const reload = () => {
}
})
} else {
alert ('Unable to reload the active tab — please add the "activeTab" permission to manifest.json!')
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.management.getSelf (self => {

if (self.installType === 'development') {

chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
}
})
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "An example extension which demonstrates hot-reloading",
"version": "1.1337.0",

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

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

0 comments on commit e7a4b6d

Please sign in to comment.