Skip to content
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

add support for most search engines #31

Merged
merged 13 commits into from
Dec 9, 2022
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ChatGPT for Google

A browser extension to display ChatGPT response alongside Google Search results, supports Chrome/Edge/Firefox
A browser extension to display ChatGPT response alongside Search Engine results, supports Chrome/Edge/Firefox

<a href="https://www.buymeacoffee.com/wong2" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 55px !important;width: 217px !important;" ></a> <a href="https://www.producthunt.com/posts/chatgpt-for-google?utm_source=badge-featured" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=369975&theme=light" /></a>

Expand Down
71 changes: 71 additions & 0 deletions src/content-script/engine-match-config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @typedef {object} SiteConfig
* @property {string[]} inputQuery - for search box
* @property {string[]} sidebarContainerQuery - prepend child to
* @property {string[]} appendContainerQuery - if sidebarContainer not exists, append child to
*/
/**
* @type {Object.<string,SiteConfig>}
*/
export const config = {
google: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: ["#rhs"],
appendContainerQuery: ["#rcnt"]
},
bing: {
inputQuery: ["textarea[name='q']"],
sidebarContainerQuery: ["#b_context"],
appendContainerQuery: []
},
yahoo: {
inputQuery: ["input[name='p']"],
sidebarContainerQuery: ["#right", ".Contents__inner.Contents__inner--sub"],
appendContainerQuery: ["#cols", "#contents__wrap"]
},
duckduckgo: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: [".results--sidebar.js-results-sidebar"],
appendContainerQuery: ["#links_wrapper"]
},
startpage: {
inputQuery: ["input[name='query']"],
sidebarContainerQuery: [".layout-web__sidebar.layout-web__sidebar--web"],
appendContainerQuery: [".layout-web__body.layout-web__body--desktop"]
},
baidu: {
inputQuery: ["input[name='wd']"],
sidebarContainerQuery: ["#content_right"],
appendContainerQuery: ["#container"]
},
kagi: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: [".right-content-box._0_right_sidebar"],
appendContainerQuery: ["#_0_app_content"],
},
yandex: {
inputQuery: ["input[name='text']"],
sidebarContainerQuery: ["#search-result-aside"],
appendContainerQuery: []
},
naver: {
inputQuery: ["input[name='query']"],
sidebarContainerQuery: ["#sub_pack"],
appendContainerQuery: ["#content"]
},
brave: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: ["#side-right"],
appendContainerQuery: []
},
searx: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: ["#sidebar_results"],
appendContainerQuery: []
},
ecosia: {
inputQuery: ["input[name='q']"],
sidebarContainerQuery: [".sidebar.web__sidebar"],
appendContainerQuery: ["#main"]
}
}
14 changes: 11 additions & 3 deletions src/content-script/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import MarkdownIt from "markdown-it";
import Browser from "webextension-polyfill";
import {getPossibleElementByQuerySelector} from "./utils.mjs"
import {config} from "./engine-match-config.mjs"

async function run(question) {
const markdown = new MarkdownIt();
Expand All @@ -8,12 +10,14 @@ async function run(question) {
container.className = "chat-gpt-container";
container.innerHTML = '<p class="loading">Waiting for ChatGPT response...</p>';

const siderbarContainer = document.getElementById("rhs");
const siderbarContainer = getPossibleElementByQuerySelector(config[siteName].sidebarContainerQuery);
if (siderbarContainer) {
siderbarContainer.prepend(container);
} else {
container.classList.add("sidebar-free");
document.getElementById("rcnt").appendChild(container);
const appendContainer = getPossibleElementByQuerySelector(config[siteName].appendContainerQuery);
if (appendContainer)
appendContainer.appendChild(container);
}

const port = Browser.runtime.connect();
Expand All @@ -32,7 +36,11 @@ async function run(question) {
port.postMessage({ question });
}

const searchInput = document.getElementsByName("q")[0];
const matchedSites = Object.keys(config);
const siteRegex = new RegExp(`(${matchedSites.join('|')})`);
const siteName = document.location.hostname.match(siteRegex)[0];

const searchInput = getPossibleElementByQuerySelector(config[siteName].inputQuery);
if (searchInput && searchInput.value) {
// only run on first page
const startParam = new URL(location.href).searchParams.get("start") || "0";
Expand Down
8 changes: 8 additions & 0 deletions src/content-script/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getPossibleElementByQuerySelector(queryArray) {
for (const query of queryArray) {
const element = document.querySelector(query);
if (element) {
return element;
}
}
}
18 changes: 16 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@
},
"content_scripts": [
{
"matches": ["https://*/search*"],
"include_globs": ["*.google.*/*"],
"matches": ["https://*/*"],
"include_globs": [
"https://*.google.*/*",
"https://kagi.*/*",
"https://*.bing.*/*",
"https://*.yahoo.*/*",
"https://*.naver.*/*",
"https://*.brave.*/*",
"https://*.ecosia.org/*",
"https://searx.be/*",
"https://www.searx.be/*",
"https://yandex.*/*",
"https://duckduckgo.*",
"https://*.startpage.*/*",
"https://*.baidu.*"
],
"js": ["content-script.js"],
"css": ["github-markdown.css", "styles.css"]
}
Expand Down
18 changes: 16 additions & 2 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@
},
"content_scripts": [
{
"matches": ["https://*/search*"],
"include_globs": ["*.google.*/*"],
"matches": ["https://*/*"],
"include_globs": [
"https://*.google.*/*",
"https://kagi.*/*",
"https://*.bing.*/*",
"https://*.yahoo.*/*",
"https://*.naver.*/*",
"https://*.brave.*/*",
"https://*.ecosia.org/*",
"https://searx.be/*",
"https://www.searx.be/*",
"https://yandex.*/*",
"https://duckduckgo.*",
"https://*.startpage.*/*",
"https://*.baidu.*"
],
"js": ["content-script.js"],
"css": ["github-markdown.css", "styles.css"]
}
Expand Down