Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add context menu support #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
var DEFAULT_CONTEXT_MENU = true;
var oldcontextMenuState = DEFAULT_CONTEXT_MENU;
var newContextMenuState = DEFAULT_CONTEXT_MENU;
var escapedSearches = {};

/* Received returnSearchInfo message, set badge text with number of results */
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if ('returnSearchInfo' == request.message) {
chrome.browserAction.setBadgeText({
'text': String(request.numResults),
'tabId': sender.tab.id
});
newContextMenuState = !!request.regexString;
if (!newContextMenuState) {
delete escapedSearches[sender.tab.id];
}
} else if ('triggerContextMenu' === request.message) {
chrome.contextMenus.update('regex-search', {
visible: (newContextMenuState = request.visible)
});
}
if (newContextMenuState != oldcontextMenuState) {
chrome.contextMenus.update('regex-search-again', {
visible: (oldcontextMenuState = newContextMenuState)
});
}
});

chrome.runtime.onInstalled.addListener(function() {
chrome.storage.local.get({
'contextMenu': DEFAULT_CONTEXT_MENU
}, function(result) {
chrome.contextMenus.update('regex-search', {
visible: result.contextMenu
});
});
chrome.contextMenus.create({
id: 'regex-search',
title: 'search /%s/ on this page',
contexts: ['selection'],
visible: false,
onclick: function(info, tab) {
chrome.tabs.sendMessage(tab.id, {
'message': 'search',
'regexString': (escapedSearches[tab.id] = info.selectionText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
});
chrome.contextMenus.update('regex-search-again', {
title: 'search next',
visible: true
});
}
});
chrome.contextMenus.create({
id: 'regex-search-again',
title: 'search again',
contexts: ['page'],
visible: false,
onclick: function(info, tab) {
chrome.tabs.sendMessage(tab.id, {
'message' : 'search',
'regexString' : escapedSearches[tab.id],
'getNext': true
});
}
});
});
17 changes: 17 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var WHITE_COLOR = '#ffffff';
var ERROR_COLOR = '#ff8989';
var GOOD_COLOR = '#89ff89';
var DEFAULT_INSTANT_RESULTS = true;
var DEFAULT_CONTEXT_MENU = true;
/*** CONSTANTS ***/

/*** FUNCTIONS ***/
Expand Down Expand Up @@ -45,6 +46,15 @@ function validateMaxResults() {
return false;
}

/* Show/hide context menu */
function triggerContextMenu(checked) {
chrome.runtime.sendMessage({
'message' : 'triggerContextMenu',
visible: !!checked
});
return checked;
}

/* Save options to storage */
function saveOptions() {
var maxResults = validateMaxResults();
Expand All @@ -54,6 +64,7 @@ function saveOptions() {
'selectedColor' : document.getElementById('selectedColor').value,
'textColor' : document.getElementById('textColor').value,
'maxResults' : maxResults,
'contextMenu': triggerContextMenu(document.getElementById('contextMenu').checked),
'instantResults' : document.getElementById('instantResults').checked,
'maxHistoryLength' : document.getElementById('maxHistoryLength').value
}
Expand All @@ -71,6 +82,7 @@ function loadOptions() {
'selectedColor' : DEFAULT_SELECTED_COLOR,
'textColor' : DEFAULT_TEXT_COLOR,
'maxResults' : DEFAULT_MAX_RESULTS,
'contextMenu': DEFAULT_CONTEXT_MENU,
'instantResults' : DEFAULT_INSTANT_RESULTS,
'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH },
function(result) {
Expand All @@ -82,6 +94,7 @@ function loadOptions() {
document.getElementById('exampleHighlighted').style.color = result.textColor;
document.getElementById('exampleSelected').style.color = result.textColor;
document.getElementById('maxResults').value = result.maxResults;
document.getElementById('contextMenu').checked = triggerContextMenu(result.contextMenu);
document.getElementById('instantResults').checked = result.instantResults;
document.getElementById('maxHistoryLength').value = result.maxHistoryLength;
}
Expand Down Expand Up @@ -128,6 +141,10 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('maxHistoryLength').addEventListener('change', function() {
saveOptions();
});

document.getElementById('contextMenu').addEventListener('change', function() {
saveOptions();
});

document.getElementById('buttonSave').addEventListener('click', function() {
saveOptions();
Expand Down
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"permissions": [
"activeTab",
"contextMenus",
"storage"
],
"background": {
Expand Down
8 changes: 8 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
<small>The default number of records is 30.</small>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Would you like to use 'search /regex/' in the context menu?</div>
<div class="panel-body">
<input id="contextMenu" type="checkbox" /> <label for="contextMenu">Context menu button</label>
<br />
<small>Can be very useful if like to avoid using your keyboard too often.</small>
</div>
</div>
<button id="buttonSave" type="save">Save Settings</button>
<button id="buttonReset" type="reset">Reset to Defaults</button>
<p id="status" class="status-chrome-regex"></p>
Expand Down