generated from fregante/browser-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue where content script was not loading on link clicks
- Loading branch information
Showing
6 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type Manifest from 'webextension-manifest'; | ||
// eslint-disable-next-line import/no-unassigned-import | ||
import './options-storage.js'; | ||
|
||
const perTabIdLastUrl = {}; | ||
|
||
// This is needed because github is doing some slippery things where the content script | ||
// isn't loaded on link clicks due to SPA. This code listens to those sort of | ||
// navigations and runs the content script once per url per tab | ||
chrome.webNavigation.onHistoryStateUpdated.addListener( | ||
(details: chrome.webNavigation.WebNavigationTransitionCallbackDetails) => { | ||
console.log( | ||
`Detected navigation in SPA on tab ${details.tabId} for url ${details.url}`, | ||
); | ||
if ( | ||
details.tabId in perTabIdLastUrl && | ||
perTabIdLastUrl[details.tabId] === details.url | ||
) { | ||
console.log('doing nothing since this is an extra event'); | ||
return; | ||
} | ||
|
||
perTabIdLastUrl[details.tabId] = details.url; | ||
|
||
fetch('./manifest.json') | ||
.then(async (response) => response.json()) | ||
.then((manifest: Manifest) => { | ||
const hashedJsFile = manifest.content_scripts[0].js[0]; | ||
|
||
chrome.scripting | ||
.executeScript({ | ||
target: {tabId: details.tabId}, | ||
files: [hashedJsFile], | ||
}) | ||
.catch((error: unknown) => { | ||
console.error(error); | ||
}); | ||
}) | ||
.catch((error: unknown) => { | ||
console.error(error); | ||
}); | ||
}, | ||
); |
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
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