-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from aaron5670/feature/service-worker-with-con…
…text-menu Feature: Added service worker that listens to a dynamic ContextMenu
- Loading branch information
Showing
7 changed files
with
112 additions
and
50 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { setLocalStorageValue } from "~local-storage-injector"; | ||
|
||
const broadcastChannel = new BroadcastChannel('broadcastChannel'); | ||
|
||
broadcastChannel.onmessage = (event) => { | ||
const { data } = event; | ||
const { history, localStorageKey } = data; | ||
|
||
// set the localStorageKey to the one that was sent from the content script | ||
if (localStorageKey) { | ||
chrome.storage.local.set({ | ||
"localStorageKey": localStorageKey, | ||
}, function () { | ||
console.log(`localStorageKey is set to '${localStorageKey}'`); | ||
}); | ||
} | ||
|
||
if(history?.length > 1) { | ||
// first remove all the context menus | ||
chrome.contextMenus.removeAll(); | ||
|
||
// then create the new ones | ||
history.forEach((item) => { | ||
chrome.contextMenus.create({ | ||
id: item.key, | ||
title: `${item.name} (${item.key})`, | ||
type: "normal", | ||
contexts: ["all"], | ||
}); | ||
}) | ||
} else { | ||
chrome.contextMenus.removeAll(); | ||
} | ||
} | ||
|
||
chrome.contextMenus.onClicked.addListener(async (info, tabs) => { | ||
chrome.storage.local.get(['localStorageKey'], async function(result) { | ||
await chrome.scripting.executeScript( | ||
{ | ||
target: { tabId: tabs.id }, | ||
world: "MAIN", // MAIN in order to access the window object | ||
func: setLocalStorageValue, | ||
args: [result.localStorageKey, info.menuItemId] | ||
} | ||
) | ||
}); | ||
}); |
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
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
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