Skip to content

V3 migration #74

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

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* 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({
chrome.action.setBadgeText({
'text': String(request.numResults),
'tabId': sender.tab.id
});
Expand Down
44 changes: 19 additions & 25 deletions src/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,23 @@ function search(regexString, configurationChanged) {
'selectedColor' : DEFAULT_SELECTED_COLOR,
'textColor' : DEFAULT_TEXT_COLOR,
'maxResults' : DEFAULT_MAX_RESULTS,
'caseInsensitive' : DEFAULT_CASE_INSENSITIVE},
function(result) {
initSearchInfo(regexString);
if(result.caseInsensitive){
regex = new RegExp(regexString, 'i');
}
highlight(regex, result.highlightColor, result.selectedColor, result.textColor, result.maxResults);
selectFirstNode(result.selectedColor);
returnSearchInfo('search');
'caseInsensitive' : DEFAULT_CASE_INSENSITIVE
}).then((result) => {
initSearchInfo(regexString);
if(result.caseInsensitive){
regex = new RegExp(regexString, 'i');
}
);
highlight(regex, result.highlightColor, result.selectedColor, result.textColor, result.maxResults);
selectFirstNode(result.selectedColor);
returnSearchInfo('search');
});
} else if (regex && regexString != '' && regexString === searchInfo.regexString) { // elements are already highlighted
chrome.storage.local.get({
'highlightColor' : DEFAULT_HIGHLIGHT_COLOR,
'selectedColor' : DEFAULT_SELECTED_COLOR},
function(result) {
selectNextNode(result.highlightColor, result.selectedColor);
}
);
'selectedColor' : DEFAULT_SELECTED_COLOR
}).then((result) => {
selectNextNode(result.highlightColor, result.selectedColor);
});
} else { // blank string or invalid regex
removeHighlight();
initSearchInfo(regexString);
Expand All @@ -227,22 +225,18 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
chrome.storage.local.get({
'highlightColor' : DEFAULT_HIGHLIGHT_COLOR,
'selectedColor' : DEFAULT_SELECTED_COLOR
},
function(result) {
selectNextNode(result.highlightColor, result.selectedColor);
}
);
}).then((result) => {
selectNextNode(result.highlightColor, result.selectedColor);
});
}
/* Received selectPrevNode message, select previous regex match */
else if ('selectPrevNode' == request.message) {
chrome.storage.local.get({
'highlightColor' : DEFAULT_HIGHLIGHT_COLOR,
'selectedColor' : DEFAULT_SELECTED_COLOR
},
function(result) {
selectPrevNode(result.highlightColor, result.selectedColor);
}
);
}).then((result) => {
selectPrevNode(result.highlightColor, result.selectedColor);
});
}
else if ('copyToClipboard' == request.message) {
var clipboardHelper = document.createElement('textarea');
Expand Down
31 changes: 15 additions & 16 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function saveOptions() {
'maxHistoryLength' : document.getElementById('maxHistoryLength').value
}

chrome.storage.local.set(options, function() {
chrome.storage.local.set(options).then(() => {
markStatus('New settings saved');
});
}
Expand All @@ -72,25 +72,24 @@ function loadOptions() {
'textColor' : DEFAULT_TEXT_COLOR,
'maxResults' : DEFAULT_MAX_RESULTS,
'instantResults' : DEFAULT_INSTANT_RESULTS,
'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH },
function(result) {
document.getElementById('highlightColor').value = result.highlightColor;
document.getElementById('exampleHighlighted').style.backgroundColor = result.highlightColor;
document.getElementById('selectedColor').value = result.selectedColor;
document.getElementById('exampleSelected').style.backgroundColor = result.selectedColor;
document.getElementById('textColor').value = result.textColor;
document.getElementById('exampleHighlighted').style.color = result.textColor;
document.getElementById('exampleSelected').style.color = result.textColor;
document.getElementById('maxResults').value = result.maxResults;
document.getElementById('instantResults').checked = result.instantResults;
document.getElementById('maxHistoryLength').value = result.maxHistoryLength;
}
);
'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH
}).then((result) => {
document.getElementById('highlightColor').value = result.highlightColor;
document.getElementById('exampleHighlighted').style.backgroundColor = result.highlightColor;
document.getElementById('selectedColor').value = result.selectedColor;
document.getElementById('exampleSelected').style.backgroundColor = result.selectedColor;
document.getElementById('textColor').value = result.textColor;
document.getElementById('exampleHighlighted').style.color = result.textColor;
document.getElementById('exampleSelected').style.color = result.textColor;
document.getElementById('maxResults').value = result.maxResults;
document.getElementById('instantResults').checked = result.instantResults;
document.getElementById('maxHistoryLength').value = result.maxHistoryLength;
});
}

/* Restore default configuration */
function restoreDefaults() {
chrome.storage.local.clear(function() {
chrome.storage.local.clear().then(() => {
markStatus('Defaults restored');
});
loadOptions();
Expand Down
61 changes: 28 additions & 33 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ function selectNext(){
chrome.tabs.query({
'active': true,
'currentWindow': true
},
function(tabs) {
}).then((tabs) => {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
'message' : 'selectNextNode'
Expand All @@ -51,8 +50,7 @@ function selectPrev(){
chrome.tabs.query({
'active': true,
'currentWindow': true
},
function(tabs) {
}).then((tabs) => {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
'message' : 'selectPrevNode'
Expand All @@ -74,21 +72,20 @@ function passInputToContentScript(configurationChanged){
} else {
document.getElementById('inputRegex').style.backgroundColor = WHITE_COLOR;
}
chrome.tabs.query(
{ 'active': true, 'currentWindow': true },
function(tabs) {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
processingKey = true;
chrome.tabs.sendMessage(tabs[0].id, {
'message' : 'search',
'regexString' : regexString,
'configurationChanged' : configurationChanged,
'getNext' : true
});
sentInput = true;
}
chrome.tabs.query({
'active': true, 'currentWindow': true
}).then((tabs) => {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
processingKey = true;
chrome.tabs.sendMessage(tabs[0].id, {
'message' : 'search',
'regexString' : regexString,
'configurationChanged' : configurationChanged,
'getNext' : true
});
sentInput = true;
}
);
});
}
}

Expand Down Expand Up @@ -174,17 +171,18 @@ function setHistoryVisibility(makeVisible) {
}

function setCaseInsensitiveElement() {
var caseInsensitive = chrome.storage.local.get({'caseInsensitive':DEFAULT_CASE_INSENSITIVE},
function (result) {
chrome.storage.local.get({
'caseInsensitive':DEFAULT_CASE_INSENSITIVE
}).then((result) => {
document.getElementById('insensitive').title = result.caseInsensitive ? DISABLE_CASE_INSENSITIVE_TITLE : ENABLE_CASE_INSENSITIVE_TITLE;
if(result.caseInsensitive) {
document.getElementById('insensitive').className = 'selected';
} else {
document.getElementById('insensitive').className = '';
}
});

}

function toggleCaseInsensitive() {
var caseInsensitive = document.getElementById('insensitive').className == 'selected';
document.getElementById('insensitive').title = caseInsensitive ? ENABLE_CASE_INSENSITIVE_TITLE : DISABLE_CASE_INSENSITIVE_TITLE;
Expand Down Expand Up @@ -235,8 +233,7 @@ document.getElementById('copy-to-clipboard').addEventListener('click', function
chrome.tabs.query({
'active': true,
'currentWindow': true
},
function (tabs) {
}).then((tabs) => {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
'message': 'copyToClipboard'
Expand Down Expand Up @@ -271,15 +268,15 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
* http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once */
var map = [];
onkeydown = onkeyup = function(e) {
map[e.keyCode] = e.type == 'keydown';
map[e.key] = e.type == 'keydown';
if (document.getElementById('inputRegex') === document.activeElement) { //input element is in focus
if (!map[16] && map[13]) { //ENTER
if (!map['Shift'] && map['Enter']) { //ENTER
if (sentInput) {
selectNext();
} else {
passInputToContentScript();
}
} else if (map[16] && map[13]) { //SHIFT + ENTER
} else if (map['Shift'] && map['Enter']) { //SHIFT + ENTER
selectPrev();
}
}
Expand All @@ -292,8 +289,8 @@ chrome.storage.local.get({
'instantResults' : DEFAULT_INSTANT_RESULTS,
'maxHistoryLength' : MAX_HISTORY_LENGTH,
'searchHistory' : null,
'isSearchHistoryVisible' : false},
function(result) {
'isSearchHistoryVisible' : false
}).then((result) => {
if(result.instantResults) {
document.getElementById('inputRegex').addEventListener('input', function() {
passInputToContentScript();
Expand All @@ -314,19 +311,17 @@ chrome.storage.local.get({
}
setHistoryVisibility(result.isSearchHistoryVisible);
updateHistoryDiv();
}
);
});

/* Get search info if there is any */
chrome.tabs.query({
'active': true,
'currentWindow': true
},
function(tabs) {
}).then((tabs) => {
if ('undefined' != typeof tabs[0].id && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
'message' : 'getSearchInfo'
}, function(response){
}).then((response) => {
if (response) {
// Content script is active
console.log(response);
Expand Down
9 changes: 4 additions & 5 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,

"name": "Chrome Regex Search",
"description": "An extension for Regex Search in lieu of Chrome's CTRL+F",
Expand All @@ -15,7 +15,7 @@
"128": "icons/icons_128.png",
"256": "icons/icons_256.png"
},
"browser_action": {
"action": {
"default_icon": {
"16": "icons/icons_16.png",
"24": "icons/icons_24.png",
Expand All @@ -34,8 +34,7 @@
"storage"
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
"service_worker": "js/background.js"
},
"content_scripts": [
{
Expand All @@ -44,7 +43,7 @@
}
],
"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"windows": "Ctrl+Shift+F",
"mac": "Command+Shift+F",
Expand Down