-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
91 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 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,40 @@ | ||
import browser from "webextension-polyfill"; | ||
import { KEY_PREFIX } from "../constants"; | ||
import { StorageChange } from "../types"; | ||
|
||
|
||
|
||
export const handleStorageChange = async (changes: { | ||
[index: string]: StorageChange | ||
}) => { | ||
{ | ||
const changedArray: StorageChange[] = Object.values(changes) | ||
|
||
if(!changedArray?.length) return null; | ||
|
||
changedArray.forEach(({oldValue, newValue}: StorageChange) => { | ||
// ADD KEY | ||
if(!oldValue && newValue) { | ||
console.log({'create': true, oldValue, newValue}); | ||
browser.contextMenus.create({ | ||
title: `Search on ${newValue.url}`, | ||
contexts: ["selection"], | ||
id: `${KEY_PREFIX}-${newValue.id}`, | ||
}); | ||
} | ||
// REMOVE KEY | ||
else if (oldValue && !newValue){ | ||
console.log({'REMOVE': true, oldValue, newValue}); | ||
browser.contextMenus.remove(oldValue.id); | ||
} | ||
// EDIT KEY | ||
else if (oldValue && newValue){ | ||
console.log({'EDIT': true, oldValue, newValue}); | ||
browser.contextMenus.update(oldValue.id, { | ||
title: `Search on ${newValue.url}`, | ||
contexts: ["selection"], | ||
}); | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,50 +1,70 @@ | ||
import browser from "webextension-polyfill"; | ||
import browser, { Menus } from "webextension-polyfill"; | ||
import { keyLists } from "./superkey.db.json"; | ||
import { handleStorageChange } from "./events"; | ||
import { KEY_PREFIX } from "../constants"; | ||
|
||
const KEY_PREFIX = "superkey"; | ||
interface IDefaultMenu extends Menus.CreateCreatePropertiesType{ | ||
// for future | ||
} | ||
|
||
const DEFAULT_CONTEXT_MENUS: IDefaultMenu[] = [ | ||
{ | ||
title: `Manage Keys`, | ||
contexts: ["all"], | ||
id: `addNew`, | ||
} | ||
] | ||
|
||
const handleDefaultMenuClick = async (id: string) => { | ||
const defaultKey = DEFAULT_CONTEXT_MENUS.find(each => each.id === id) | ||
let urlToGo:string = ''; | ||
if(defaultKey?.id === 'addNew') | ||
urlToGo = browser.runtime.getURL('dist/options/index.html') | ||
|
||
return urlToGo && browser.tabs.create({ | ||
url: urlToGo, | ||
}); | ||
} | ||
|
||
function handleSearch(info: any, tab: any) { | ||
async function handleSearch(info: any, tab: any) { | ||
const [keyPrefix, indexKey] = info.menuItemId.split("-"); | ||
|
||
if (keyPrefix !== KEY_PREFIX || !info.selectionText) return; | ||
if (keyPrefix !== KEY_PREFIX) return; | ||
|
||
await handleDefaultMenuClick(indexKey) | ||
|
||
if(!info.index) return; | ||
|
||
browser.storage.sync.get(indexKey).then((item) => { | ||
const keyItem = item[indexKey]; | ||
|
||
browser.tabs.create({ | ||
return keyItem && browser.tabs.create({ | ||
url: `${keyItem.queryUrl}=${encodeURI(info.selectionText)}`, | ||
}); | ||
}); | ||
} | ||
|
||
browser.runtime.onInstalled.addListener(async () => { | ||
// CLEAN ALL CONTEXT MENU BEFORE ADDING/RESETTING ALL KEYS | ||
await browser.contextMenus.removeAll() | ||
|
||
// Add Default Menus | ||
DEFAULT_CONTEXT_MENUS.forEach(({title,contexts,id }:Menus.CreateCreatePropertiesType) => | ||
browser.contextMenus.create({ | ||
title,contexts,id: `${KEY_PREFIX}-${id}` | ||
})) | ||
|
||
// Add User Keys | ||
keyLists.forEach((keyItem, index) => { | ||
const indexKey = index + 1; | ||
browser.storage.sync | ||
// @ts-ignore | ||
.set({ [indexKey]: { id: indexKey, ...keyItem } }) // TODO: Test carefully. | ||
.then(() => browser.runtime.openOptionsPage()); | ||
|
||
// context menu | ||
if (keyItem.queryUrl) { | ||
browser.contextMenus.create({ | ||
title: `Search on ${keyItem.url}`, | ||
contexts: ["selection"], | ||
id: `${KEY_PREFIX}-${indexKey}`, | ||
}); | ||
} | ||
// This will trigger handleStorageChange | ||
}); | ||
}); | ||
|
||
browser.contextMenus.onClicked.addListener(handleSearch); | ||
|
||
browser.storage.onChanged.addListener(function (changes) { | ||
const { newValue } = Object.values(changes)?.[0] || {}; | ||
|
||
if (newValue.queryUrl) | ||
browser.contextMenus.create({ | ||
title: `Search on ${newValue.url}`, | ||
contexts: ["selection"], | ||
id: `${KEY_PREFIX}-${newValue.id}`, | ||
}); | ||
}); | ||
browser.storage.onChanged.addListener(handleStorageChange); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const KEY_PREFIX = "superkey"; |
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