Skip to content

Commit

Permalink
Merge pull request #33 from ninoseki/context-menu-improvement
Browse files Browse the repository at this point in the history
Switch to clear the context menus in `document.onselectstart`
  • Loading branch information
ninoseki authored Jul 21, 2018
2 parents 5743628 + 84315ed commit f69fe6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,40 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
});

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.request === "removeContextMenu") {
chrome.contextMenus.removeAll();
}

if (message.request === "updateContextMenu") {
chrome.contextMenus.removeAll(() => {
// search searchers based on a type of the input
const text: string = message.selection;
const selector: Selector = new Selector(text);
const searcherEntries: AnalyzerEntry[] = selector.getSearcherEntries();
for (const entry of searcherEntries) {
const name = entry.analyzer.name;
// it tells action/query/type/target to the listner
const id = `Search ${entry.query} as a ${entry.type} on ${name}`;
const title = `Search this ${entry.type} on ${name}`;
const options = {
contexts: ["selection"],
id,
title,
};
chrome.contextMenus.create(options);
}
// search scanners based on a type of the input
const scannerEntries: AnalyzerEntry[] = selector.getScannerEntries();
for (const entry of scannerEntries) {
const name = entry.analyzer.name;
// it tells action/query/type/target to the listner
const id = `Scan ${entry.query} as a ${entry.type} on ${name}`;
const title = `Scan this ${entry.type} on ${name}`;
const options = {
contexts: ["selection"],
id,
title,
};
chrome.contextMenus.create(options);
}
});
// search searchers based on a type of the input
const text: string = message.selection;
const selector: Selector = new Selector(text);
const searcherEntries: AnalyzerEntry[] = selector.getSearcherEntries();
for (const entry of searcherEntries) {
const name = entry.analyzer.name;
// it tells action/query/type/target to the listner
const id = `Search ${entry.query} as a ${entry.type} on ${name}`;
const title = `Search this ${entry.type} on ${name}`;
const options = {
contexts: ["selection"],
id,
title,
};
chrome.contextMenus.create(options);
}
// search scanners based on a type of the input
const scannerEntries: AnalyzerEntry[] = selector.getScannerEntries();
for (const entry of scannerEntries) {
const name = entry.analyzer.name;
// it tells action/query/type/target to the listner
const id = `Scan ${entry.query} as a ${entry.type} on ${name}`;
const title = `Scan this ${entry.type} on ${name}`;
const options = {
contexts: ["selection"],
id,
title,
};
chrome.contextMenus.create(options);
}
}
});
4 changes: 4 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ document.onselectionchange = () => {
});
}
};

document.onselectstart = () => {
chrome.runtime.sendMessage({ request: "removeContextMenu" });
};

0 comments on commit f69fe6f

Please sign in to comment.