Skip to content

Commit

Permalink
feat: Add support for google search results for en
Browse files Browse the repository at this point in the history
Show top google search result (as per assistant response) instead of
plain text for english language
  • Loading branch information
Melvin-Abraham committed Jan 1, 2023
1 parent 97bf12e commit 28d6224
Show file tree
Hide file tree
Showing 4 changed files with 2,730 additions and 1,408 deletions.
94 changes: 79 additions & 15 deletions app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ minimizeButton.onclick = () => {
const electron = require('electron');
const GoogleAssistant = require('google-assistant');
const isValidAccelerator = require('electron-is-accelerator');
const googleIt = require('google-it');
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
Expand Down Expand Up @@ -1194,6 +1195,29 @@ function inspectResponseType(assistantResponseString) {
? youtubeMatch[3].startsWith('https://m.youtube.com/watch?v=')
: false;

const googleSearchPrompts = [
'here\'s a result from search',
'here\'s a result from the web',
'here\'s the top search result',
'this came back from google',
'this came back from a search',
'here\'s what i found on the web',
'this is the top result',
'here\'s what i found',
'here\'s some info',
'this is from wikipedia',
'i found this on wikipedia',
'here\'s an answer from wikipedia',
'here\'s a wikipedia result',
'here\'s the top wikipedia result',
'wikipedia has this result',
'here\'s something from wikipedia',
'here\'s a result from wikipedia',
'here\'s a matching wikipedia result',
];

const isGoogleSearchPrompt = googleSearchPrompts.includes(mainArea.innerText.toLowerCase());

let type;
let searchResultParts;

Expand All @@ -1205,6 +1229,10 @@ function inspectResponseType(assistantResponseString) {
type = 'google-search-result';
searchResultParts = searchResultMatch.slice(1, 5);
}
else if (isGoogleSearchPrompt) {
type = 'google-search-result-prompt';
searchResultParts = null;
}
else {
type = null;
searchResultParts = null;
Expand Down Expand Up @@ -3941,6 +3969,40 @@ async function displayScreenData(screen, pushToHistory = false, theme = null) {
</div>
`;
}
else if (responseType['type'] === 'google-search-result-prompt') {
activateLoader();

const searchResults = await googleIt({
query: getCurrentQuery(),
'no-display': true,
});

const topResult = searchResults[0];

const googleSearchResultScreenData = `
<div
class="google-search-result"
data-url="${topResult.link}"
>
<div style="font-size: 22px;">
${topResult.title}
</div>
<div style="opacity: 0.502; padding-top: 5px;">
${topResult.link}
</div>
<hr color="#ffffff" style="opacity: 0.25;">
<div style="padding-top: 10px;">
${topResult.snippet}
</div>
</div>
`;

textContainer.innerHTML = googleSearchResultScreenData;
deactivateLoader();
}
}

if (innerText.indexOf('https://www.google.com/search?tbm=isch') !== -1) {
Expand Down Expand Up @@ -4074,7 +4136,7 @@ async function displayScreenData(screen, pushToHistory = false, theme = null) {
const suggestionsDOM = htmlDocument.querySelector('#assistant-scroll-bar');
const suggestionParent = document.querySelector('.suggestion-parent');

if (suggestionsDOM != null) {
if (suggestionsDOM != null || responseType['type'] === 'google-search-result-prompt') {
if (responseType['type'] || hasWebAnswer || hasKnowledgePanel) {
suggestionParent.innerHTML += `
<div class="suggestion" onclick="openLink('https://google.com/search?q=${getCurrentQuery()}')" data-flag="action-btn">
Expand Down Expand Up @@ -4146,7 +4208,7 @@ async function displayScreenData(screen, pushToHistory = false, theme = null) {
`;
}

for (let i = 0; i < suggestionsDOM.children.length; i++) {
for (let i = 0; i < suggestionsDOM?.children.length; i++) {
const label = suggestionsDOM.children[i].innerHTML.trim();
const query = suggestionsDOM.children[i].getAttribute('data-follow-up-query');
let action = query;
Expand Down Expand Up @@ -4198,7 +4260,7 @@ async function displayScreenData(screen, pushToHistory = false, theme = null) {
if (pushToHistory && mainArea.querySelector('.error-area') == null) {
let screenData;

if (isGoogleImagesContent) {
if (isGoogleImagesContent || responseType['type'] === 'google-search-result-prompt') {
screenData = generateScreenData(true);
}
else {
Expand Down Expand Up @@ -4248,20 +4310,22 @@ function generateScreenData(includePreventAutoScaleFlag = false) {
const suggestions = document.querySelector('.suggestion-parent').children;
let suggestionsDOM = '';

for (let i = 0; i < suggestions.length; i++) {
const flag = suggestions[i].getAttribute('data-flag');
const flagAttrib = flag ? `data-flag="${flag}"` : '';
const label = suggestions[i].innerHTML.trim();
if (suggestions.length > 0 && suggestions[0].classList.contains('suggestion')) {
for (let i = 0; i < suggestions.length; i++) {
const flag = suggestions[i].getAttribute('data-flag');
const flagAttrib = flag ? `data-flag="${flag}"` : '';
const label = suggestions[i].innerHTML.trim();

const followUpQuery = suggestions[i]
.getAttribute('onclick')
.replace(/assistantTextQuery\(`(.*)`\)/, '$1');
const followUpQuery = suggestions[i]
.getAttribute('onclick')
.replace(/assistantTextQuery\(`(.*)`\)/, '$1');

suggestionsDOM += `
<button data-follow-up-query="${followUpQuery}" ${flagAttrib}>
${label}
</button>
`;
suggestionsDOM += `
<button data-follow-up-query="${followUpQuery}" ${flagAttrib}>
${label}
</button>
`;
}
}

const screenDataSuggestionsHTML = `
Expand Down
Loading

1 comment on commit 28d6224

@sumeshir26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats actuallay a pretty smart fix - to intercept the useless text and then manually search again.

Please sign in to comment.