Add gh/gH to open home page

jh-changes
Shin'ya Ueoka 6 years ago
parent b735317c60
commit 1f89464e42
  1. 2
      src/background/controllers/operation.js
  2. 2
      src/background/usecases/command.js
  3. 13
      src/background/usecases/operation.js
  4. 2
      src/content/urls.js
  5. 1
      src/shared/operations.js
  6. 2
      src/shared/settings/default.js
  7. 20
      src/shared/urls.js
  8. 36
      test/shared/urls.test.js

@ -37,6 +37,8 @@ export default class OperationController {
return this.operationInteractor.duplicate();
case operations.PAGE_SOURCE:
return this.operationInteractor.openPageSource();
case operations.PAGE_HOME:
return this.operationInteractor.openHome(operation.newTab);
case operations.ZOOM_IN:
return this.operationInteractor.zoomIn();
case operations.ZOOM_OUT:

@ -120,6 +120,6 @@ export default class CommandIndicator {
async urlOrSearch(keywords) {
let settings = await this.settingRepository.get();
return urls.normalizeUrl(keywords, settings.search);
return urls.searchUrl(keywords, settings.search);
}
}

@ -1,5 +1,6 @@
import TabPresenter from '../presenters/tab';
import ConsolePresenter from '../presenters/console';
import * as urls from '../../shared/urls';
const ZOOM_SETTINGS = [
0.33, 0.50, 0.66, 0.75, 0.80, 0.90, 1.00,
@ -172,5 +173,17 @@ export default class OperationInteractor {
let tab = await this.tabPresenter.getCurrent();
return this.consolePresenter.hide(tab.id);
}
async openHome(newTab) {
let tab = await this.tabPresenter.getCurrent();
let result = await browser.browserSettings.homepageOverride.get({});
let us = urls.homepageUrls(result.value);
if (us.length === 1 && !newTab) {
return this.tabPresenter.open(us[0], tab.id);
}
for (let u of us) {
this.tabPresenter.create(u, { openerTabId: tab.id });
}
}
}

@ -26,7 +26,7 @@ const paste = (win, newTab, searchSettings) => {
if (win.document.execCommand('paste')) {
let value = textarea.textContent;
let url = urls.normalizeUrl(value, searchSettings);
let url = urls.searchUrl(value, searchSettings);
browser.runtime.sendMessage({
type: messages.OPEN_URL,
url,

@ -40,6 +40,7 @@ export default {
// Page
PAGE_SOURCE: 'page.source',
PAGE_HOME: 'page.home',
// Tabs
TAB_CLOSE: 'tabs.close',

@ -52,6 +52,8 @@ export default {
"gU": { "type": "navigate.root" },
"gi": { "type": "focus.input" },
"gf": { "type": "page.source" },
"gh": { "type": "page.home" },
"gH": { "type": "page.home", "newTab": true },
"y": { "type": "urls.yank" },
"p": { "type": "urls.paste", "newTab": false },
"P": { "type": "urls.paste", "newTab": true },

@ -5,7 +5,7 @@ const trimStart = (str) => {
const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:'];
const normalizeUrl = (keywords, searchSettings) => {
const searchUrl = (keywords, searchSettings) => {
try {
let u = new URL(keywords);
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
@ -28,4 +28,20 @@ const normalizeUrl = (keywords, searchSettings) => {
return template.replace('{}', encodeURIComponent(query));
};
export { normalizeUrl };
const normalizeUrl = (url) => {
try {
let u = new URL(url);
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
return u.href;
}
} catch (e) {
// fallthrough
}
return 'http://' + url;
};
const homepageUrls = (value) => {
return value.split('|').map(normalizeUrl);
};
export { searchUrl, normalizeUrl, homepageUrls };

@ -1,7 +1,7 @@
import * as parsers from 'shared/urls';
describe("shared/commands/parsers", () => {
describe('#normalizeUrl', () => {
describe('#searchUrl', () => {
const config = {
default: 'google',
engines: {
@ -11,29 +11,47 @@ describe("shared/commands/parsers", () => {
};
it('convertes search url', () => {
expect(parsers.normalizeUrl('google.com', config))
expect(parsers.searchUrl('google.com', config))
.to.equal('http://google.com');
expect(parsers.normalizeUrl('google apple', config))
expect(parsers.searchUrl('google apple', config))
.to.equal('https://google.com/search?q=apple');
expect(parsers.normalizeUrl('yahoo apple', config))
expect(parsers.searchUrl('yahoo apple', config))
.to.equal('https://yahoo.com/search?q=apple');
expect(parsers.normalizeUrl('google apple banana', config))
expect(parsers.searchUrl('google apple banana', config))
.to.equal('https://google.com/search?q=apple%20banana');
expect(parsers.normalizeUrl('yahoo C++CLI', config))
expect(parsers.searchUrl('yahoo C++CLI', config))
.to.equal('https://yahoo.com/search?q=C%2B%2BCLI');
});
it('user default search engine', () => {
expect(parsers.normalizeUrl('apple banana', config))
expect(parsers.searchUrl('apple banana', config))
.to.equal('https://google.com/search?q=apple%20banana');
});
it('searches with a word containing a colon', () => {
expect(parsers.normalizeUrl('foo:', config))
expect(parsers.searchUrl('foo:', config))
.to.equal('https://google.com/search?q=foo%3A');
expect(parsers.normalizeUrl('std::vector', config))
expect(parsers.searchUrl('std::vector', config))
.to.equal('https://google.com/search?q=std%3A%3Avector');
});
});
describe('#normalizeUrl', () => {
it('normalize urls', () => {
expect(parsers.normalizeUrl('https://google.com/'))
.to.equal('https://google.com/');
expect(parsers.normalizeUrl('google.com'))
.to.equal('http://google.com');
});
});
describe('#homepageUrls', () => {
it('split urls', () => {
expect(parsers.homepageUrls('https://google.com/'))
.to.deep.equal(['https://google.com/']);
expect(parsers.homepageUrls('yahoo.com|https://i-beam.org/'))
.to.deep.equal(['http://yahoo.com', 'https://i-beam.org/']);
});
});
});