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

Google Scholar response cleanup #130

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/webchatgpt-3.2.4-chrome.zip
Binary file not shown.
Binary file modified build/webchatgpt-3.2.4-firefox.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion src/content-scripts/ddg_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ export async function webSearch(search: SearchRequest, numResults: number): Prom
if (response.url === `${BASE_URL}/lite/`) {
results = htmlToSearchResults(response.html, numResults)
} else {
const result = await Browser.runtime.sendMessage({
let result = await Browser.runtime.sendMessage({
type: "get_webpage_text",
url: response.url,
html: response.html
})
if (result.title && result.title === "Google Scholar") {
result = formatGoogleScholarResponse(result);
}

return [{
title: result.title,
Expand All @@ -113,3 +116,36 @@ export async function webSearch(search: SearchRequest, numResults: number): Prom

return results
}

function formatGoogleScholarResponse(result: any): any {
result.body = cleanResponseText(result.body);
return result;
}

function cleanResponseText(text: string): string {
const lines = text.split('\n');
const cleanedLines: string[] = [];

for (const line of lines) {
const cleanedLine = line
.replace(/\[.*?\]/g, '') // Remove tags like [PDF], [HTML], etc.
.replace(/https?:\/\/[^\s]+/g, ' ') // Remove URLs
.replace(/Cite\s+/g, ' ') // Remove Cite button links
.replace(/Cited by \d+?/g, ' ') // Remove citation counts
.replace(/Related articles/g, ' ') // Remove 'Related articles'
.replace(/All \d+? versions/g, ' ') // Remove version counts
.replace(/View as HTML/g, ' ') // Remove 'View as HTML'
.replace(/Fulltext via \w+/g, ' ') // Remove 'Fulltext via X'
.replace(/Cached/g, '') // Remove 'Cached'
.replace(/...Save\s+/g, ' ') // Remove Save button artifact
.replace(/\S+\.(com|org|net|uk)/g, ' ') // Remove right-joined url artifacts
.replace(/arxiv:\S+/, ' ') // Remove arxiv code
.replace(/\.\.\./g, '.') // Trim ellipsis
.replace(/\s{2,}/g, ' ') // Trim inner extra spaces
.trim();
if (cleanedLine) {
cleanedLines.push(cleanedLine);
}
}
return cleanedLines.join('\n');
}