set find keyword from background
This commit is contained in:
parent
24e72aa6e0
commit
92f8365be7
3 changed files with 35 additions and 20 deletions
|
@ -5,6 +5,7 @@
|
||||||
// NOTE: window.find is not standard API
|
// NOTE: window.find is not standard API
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/Window/find
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/find
|
||||||
|
|
||||||
|
import messages from 'shared/messages';
|
||||||
import actions from 'content/actions';
|
import actions from 'content/actions';
|
||||||
import * as consoleFrames from '../console-frames';
|
import * as consoleFrames from '../console-frames';
|
||||||
|
|
||||||
|
@ -31,11 +32,24 @@ const find = (string, backwards) => {
|
||||||
return window.find(string, caseSensitive, backwards, wrapScan);
|
return window.find(string, caseSensitive, backwards, wrapScan);
|
||||||
};
|
};
|
||||||
|
|
||||||
const findNext = (keyword, reset, backwards) => {
|
const findNext = (currentKeyword, reset, backwards) => {
|
||||||
if (reset) {
|
if (reset) {
|
||||||
window.getSelection().removeAllRanges();
|
window.getSelection().removeAllRanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let promise = Promise.resolve(currentKeyword);
|
||||||
|
if (currentKeyword) {
|
||||||
|
browser.runtime.sendMessage({
|
||||||
|
type: messages.FIND_SET_KEYWORD,
|
||||||
|
keyword: currentKeyword,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
promise = browser.runtime.sendMessage({
|
||||||
|
type: messages.FIND_GET_KEYWORD,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return promise.then((keyword) => {
|
||||||
let found = find(keyword, backwards);
|
let found = find(keyword, backwards);
|
||||||
if (!found) {
|
if (!found) {
|
||||||
window.getSelection().removeAllRanges();
|
window.getSelection().removeAllRanges();
|
||||||
|
@ -52,14 +66,15 @@ const findNext = (keyword, reset, backwards) => {
|
||||||
keyword,
|
keyword,
|
||||||
found,
|
found,
|
||||||
};
|
};
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const next = (keyword, reset) => {
|
const next = (currentKeyword, reset) => {
|
||||||
return findNext(keyword, reset, false);
|
return findNext(currentKeyword, reset, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const prev = (keyword, reset) => {
|
const prev = (currentKeyword, reset) => {
|
||||||
return findNext(keyword, reset, true);
|
return findNext(currentKeyword, reset, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
export { next, prev };
|
export { next, prev };
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import actions from 'content/actions';
|
import actions from 'content/actions';
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
keyword: '',
|
keyword: null,
|
||||||
found: false,
|
found: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import findReducer from 'content/reducers/find';
|
||||||
describe("find reducer", () => {
|
describe("find reducer", () => {
|
||||||
it('return the initial state', () => {
|
it('return the initial state', () => {
|
||||||
let state = findReducer(undefined, {});
|
let state = findReducer(undefined, {});
|
||||||
expect(state).to.have.property('keyword', '');
|
expect(state).to.have.property('keyword', null);
|
||||||
expect(state).to.have.property('found', false);
|
expect(state).to.have.property('found', false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in a new issue