Skip to content

Commit

Permalink
better active tab reloading handling, permissions check
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalik Gordon committed Dec 9, 2020
2 parents 4ee257b + ea9ff50 commit 5f7683c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 15 additions & 7 deletions hot-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ const timestampForFilesInDirectory = dir =>
files.map (f => f.name + f.lastModifiedDate).join ())

const reload = () => {

chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5

if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }

setTimeout(() => { chrome.runtime.reload () }, 500)
chrome.permissions.contains ({
permissions: ["activeTab"]
}, granted => {
if (granted) {
chrome.tabs.query ({ active: true, currentWindow: 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 the "activeTab" permission to manifest.json!')
chrome.runtime.reload ()
}
})
}


const watchChanges = (dir, lastTimestamp) => {

Expand Down
2 changes: 2 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"description": "An example extension which demonstrates hot-reloading",
"version": "1.1337.0",

"permissions": ["activeTab"],

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

0 comments on commit 5f7683c

Please sign in to comment.