-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve robustness of finding AI element
- Loading branch information
Showing
4 changed files
with
58 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { | ||
if ( | ||
tab.url && | ||
tab.url.includes("://www.google.com/search") && | ||
changeInfo.status == "loading" | ||
) { | ||
chrome.storage.local.get(["overviewClass"], function (result) { | ||
if (result.overviewClass) { | ||
// If an ID is stored, inject CSS to hide the element with that ID | ||
const classNames = result.overviewClass.split(" "); | ||
const cssCode = classNames | ||
.map((className) => `.${className} { display: none !important; }`) | ||
.join(" "); | ||
chrome.scripting.insertCSS( | ||
{ | ||
target: { tabId: tabId }, | ||
css: cssCode, | ||
}, | ||
() => { | ||
console.log(`AI Overview class "${result.overviewClass}" hidden`); | ||
} | ||
); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
//Backup script if content is every dynamically loaded | ||
//or if CSS fails for whatever reason... | ||
var headers = document.querySelectorAll("h1"); | ||
|
||
var AIheaders = document.querySelectorAll("h1"); | ||
var AIparentElement = document.querySelector(".M8OgIe"); | ||
var AIparentElementPreLoad = document.querySelector(".rEow3c"); | ||
function findParentWithCorrectAttributes(element) { | ||
while (element && element.parentElement) { | ||
element = element.parentElement; | ||
if (element.hasAttribute("jsname") || element.hasAttribute("id")) { | ||
return element; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
if ( | ||
AIparentElementPreLoad && | ||
AIparentElementPreLoad.firstChild.tagName === "H1" && | ||
AIparentElementPreLoad.firstChild.textContent.trim() === "Search Results" | ||
) { | ||
AIparentElementPreLoad.style.display = "none"; | ||
} else if ( | ||
AIparentElement && | ||
AIparentElement.firstChild.tagName === "H1" && | ||
AIparentElement.firstChild.textContent.trim() === "Search Results" | ||
) { | ||
AIparentElement.style.display = "none"; | ||
} else { | ||
// Iterate through each h1 element to find the one with the exact text "AI Overview" | ||
Array.from(AIheaders).forEach(function (header) { | ||
if (header.textContent.trim() === "AI Overview") { | ||
// Iterate through each h1 element to find the one with the exact text "AI Overview" | ||
Array.from(headers).forEach(function (header) { | ||
if (header.textContent.trim() === "AI Overview") { | ||
const parent = findParentWithCorrectAttributes(header); | ||
if (parent) { | ||
parent.style.display = "none"; | ||
} else { | ||
header.parentNode.style.display = "none"; | ||
} | ||
}); | ||
} | ||
|
||
if (parent.className || header.parentNode.className) { | ||
chrome.storage.local.set({ | ||
"overviewClass": parent.className || header.parentNode.className, | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.