Skip to content

Commit

Permalink
Added tab to search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
noamalffasy committed May 9, 2017
1 parent 696189f commit f01d98f
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 1 deletion.
31 changes: 31 additions & 0 deletions app/src/js/pages/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ function defaultFunc() {
document.addEventListener('click', () => {
ipcRenderer.sendToHost('click')
});

const xmlURL = document.head.querySelector('link[type="application/opensearchdescription+xml"]').getAttribute('href');

if (xmlURL.length > 0) {
const xhr = new XMLHttpRequest();
xhr.open("GET", xmlURL, true);

xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
const xml = xhr.responseXML;

const searchURL = xml.querySelector("Url:not([rel=\"suggestions\"])").getAttribute("template");
let suggestionsURL: any = xml.querySelector("Url[rel=\"suggestions\"]")
if(suggestionsURL !== null) {
suggestionsURL = suggestionsURL.getAttribute("template");
} else {
suggestionsURL = xml.querySelector("Url[type=\"application/x-suggestions+json\"]");
if(suggestionsURL !== null) {
suggestionsURL = suggestionsURL.getAttribute('template');
}
}
const description = xml.getElementsByTagName("ShortName")[0].textContent;

ipcRenderer.sendToHost('searchURL', searchURL, description, suggestionsURL);
}
}
}

xhr.send();
}
}

document.addEventListener('DOMContentLoaded', () => {
Expand Down
179 changes: 178 additions & 1 deletion app/src/js/url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
import * as normalizeUrl from 'normalize-url';
import * as webview from './webview';
import * as settings from '../settings';

let searching = false;
let searchURL: any;
let suggestionsURL: any;

$('.bottombar .navigation .url').on('keydown', (e: any) => {
if (e.keyCode === 9) {
e.preventDefault();

if (!searching) {
const url = normalizeUrl($('.bottombar .navigation .url').text());
const http = new XMLHttpRequest();
http.open('GET', url);
http.onreadystatechange = function () {
if (this.readyState == this.DONE) {
if (this.status === 200) {
searchInUrl(this.response);
}
}
};
http.send();
}
}

if (e.keyCode === 8 && searching && $('.bottombar .navigation .url').text() === '') {
$('.bottombar .navigation .searchOn').remove();
$('.bottombar .navigation input.info, .bottombar .navigation input.secure, .bottombar .navigation input.mybrowser').removeClass('hide');
searching = false;
}
});

$('.bottombar .navigation .url').on('awesomplete-select', (e: any) => {
if (searching) {
e.preventDefault();

const input = $('.bottombar .navigation .url').text().replace(settings.selectedSearchEngine.searchUrl, '');
let url = searchURL;

url = url.replace(/{searchTerms}/g, input);
url = url.replace(/&(\w*)={(\w*)\?}/g, '');

console.log(input);

$('.navigation .url').html(url);
(document.querySelector('.page.active') as any).loadURL(url);

webview.onNavigating();

$('.bottombar .navigation .searchOn').remove();
$('.bottombar .navigation input.info, .bottombar .navigation input.secure, .bottombar .navigation input.mybrowser').removeClass('hide');
$('.bottombar .navigation #autocomplete').attr('hidden', '');
$('.bottombar .navigation #autocomplete').html('');
searching = false;
}
});

$('.bottombar .navigation .url').on('input', getSuggestionsSearch);

export function styleUrl() {
const url = $('.bottombar .navigation .url').text()
Expand Down Expand Up @@ -55,5 +112,125 @@ export function isSecure() {
}
}

export function addListenerSearchInUrl(wv: any) {
// wv.addEventListener('ipc-message', (e: any) => {
// if (e.channel === 'searchURL') {
// searchInUrl({ e: e })
// }
// });
}

export function searchInUrl(head: any) {
const hostname = $('.bottombar .navigation .url').text();

let url: string = null;
let suggestions: any = null;
let description: any = null;
const domParser = new DOMParser;
const dom = domParser.parseFromString(head, "text/html");

getSearchURLs(dom, (urls: any) => {
url = urls.searchURL;
suggestions = urls.suggestionsURL;
description = `Search ${urls.description}` || `Search ${hostname}`;

searching = true;
suggestionsURL = suggestions;
searchURL = url;

$('.bottombar .navigation .url').before(`<span class="searchOn">${description}</span>`);
$('.bottombar .navigation input.info, .bottombar .navigation input.secure, .bottombar .navigation input.mybrowser').addClass('hide');
$('.bottombar .navigation #autocomplete').attr('hidden', '');
$('.bottombar .navigation .url').html('');
});
}

export function getSuggestionsSearch() {
if (searching) {
const input = $('.bottombar .navigation .url').text();

let url = searchURL;
let suggestions = suggestionsURL;

if ((suggestions !== null) && (suggestions !== undefined)) {
suggestions = suggestions.replace(/{searchTerms}/g, input);

get(suggestions, (data: any) => {
let suggestions = JSON.parse(data);

suggestions = suggestions[1];
suggestions.unshift(input);

suggestions.forEach((item: any) => {
const li = document.createElement('li');
li.innerHTML = item;
document.querySelector('#autocomplete').appendChild(li);
});
});
}

url = url.replace(/{searchTerms}/g, input);
url = url.replace(/&(\w*)={(\w*)\?}/g, '');
}
}

export function getSearchURLs(doc: any, callback: any) {
const xmlURL = doc.head.querySelector('link[type="application/opensearchdescription+xml"]').getAttribute('href');

if (xmlURL.length > 0) {
const xhr = new XMLHttpRequest();
if (xmlURL.startsWith('http')) {
xhr.open("GET", xmlURL, true);
} else {
const url = normalizeUrl($('.bottombar .navigation .url').text());
if (xmlURL.startsWith('.')) {
xmlURL.replace('.', '')
}
xhr.open("GET", url + xmlURL, true);
}

xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
const xml = xhr.responseXML;

const searchURL = xml.querySelector("Url:not([rel=\"suggestions\"])").getAttribute("template");
let suggestionsURL: any = xml.querySelector("Url[rel=\"suggestions\"]")
if (suggestionsURL !== null) {
suggestionsURL = suggestionsURL.getAttribute("template");
} else {
suggestionsURL = xml.querySelector("Url[type=\"application/x-suggestions+json\"]");
if (suggestionsURL !== null) {
suggestionsURL = suggestionsURL.getAttribute('template');
}
}
const description = xml.getElementsByTagName("ShortName")[0].textContent;

callback({ searchURL: searchURL, description: description, suggestionsURL: suggestionsURL });
}
}
}

xhr.send();
}
}

function get(url: string, callback: any) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);

xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
callback(xhr.response);
}
}
}

xhr.send();
}

module.exports.styleUrl = styleUrl;
module.exports.isSecure = isSecure;
module.exports.isSecure = isSecure;
module.exports.searchInUrl = searchInUrl;
module.exports.addListenerSearchInUrl = addListenerSearchInUrl;
1 change: 1 addition & 0 deletions app/src/js/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export function onWebViewCreated(id: Number = undefined) {
contextMenu.onDevToolsOpen();
contextMenu.onOpenInNewTab();
contextMenu.onContextMenu();
urlModule.addListenerSearchInUrl(wv);
onHoverLink(id);
onOpenView(id);
onSearch();
Expand Down
12 changes: 12 additions & 0 deletions app/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ input,
-webkit-app-region: no-drag!important;
}

.hide {
display: none!important;
}

.navbar {
background: #f5f5f5;
cursor: default;
Expand Down Expand Up @@ -419,6 +423,14 @@ li.nav-item:not(.active):hover a.tab-close {
color: #fff;
}

span.searchOn {
background: #fff;
border: 1px solid #c3c3c3;
border-right: 0;
padding: 0 .3rem 0 .5rem;
color: #4597ce;
}

.bottombar .navigation .url {
flex: 1 1 0;
height: 1.7rem;
Expand Down

0 comments on commit f01d98f

Please sign in to comment.