-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcontent.js
More file actions
200 lines (161 loc) · 5.03 KB
/
Copy pathcontent.js
File metadata and controls
200 lines (161 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// AI Overview text patterns for different languages
const AI_OVERVIEW_PATTERNS = [
/übersicht mit ki/i, // de
/ai overview/i, // en
/prezentare generală generată de ai/i, // ro
/AI による概要/, // ja
/Обзор от ИИ/, // ru
/AI 摘要/, // zh-TW
/AI-overzicht/i, // nl
/AI-oversigt/i, // da
/Vista creada con IA/i, // es
/Přehled od AI/i, // cz
/Aperçu IA/i, // fr
];
// CSS selectors for AI overview containers
const AI_OVERVIEW_SELECTORS = {
SEARCH_RESULT_SELECTOR: "div#rso > div",
ABOVE_SEARCH_RESULTS_SELECTOR: "div#rcnt > div",
};
// Main DOM selectors
const DOM_SELECTORS = {
MAIN_BODY: "div#rcnt",
MAIN_BODY_MOBILE: "div#center_col",
HEADER_TABS: "div#hdtb-sc > div",
MAIN_ELEMENT: '[role="main"]',
PEOPLE_ALSO_ASK: "div.related-question-pair",
TABS_LIST: '[role="list"]',
THINGS_TO_KNOW: "[data-maindata]",
};
// CSS values
const CSS_VALUES = {
HIDDEN: "none",
HEADER_PADDING: "12px",
MAIN_MARGIN: "24px",
};
// Tab patterns
const TAB_PATTERNS = {
AI_MODE: /^AI Mode$/i,
};
// For each matching heading, check both possible div containers
function getAiContainerParent(startEl) {
let el = startEl;
while (el && el.parentElement) {
const parent = el.parentElement;
if (parent.id === "rso") {
//search results container
return el;
}
if (parent.id === "rcnt") {
//traditional banner container
return el;
}
el = parent;
}
return startEl;
}
function getThingsToKnowContainer(startEl) {
let el = startEl;
while (el && el.parentElement) {
const parent = el.parentElement;
if (parent.parentElement?.id === "rso") {
return el;
}
el = parent;
}
}
const getAiOverview = (mainBody) => {
if (!mainBody) return null;
const inner =
mainBody.querySelector('[data-async-type="folsrch"]') ||
mainBody.querySelector('[id^="folsrch"]');
if (inner) {
const block = getAiContainerParent(inner);
if (block) {
return block;
}
}
// Specific AI overview language based fallback
// const aiTexts = [
// ...mainBody.querySelectorAll("h1, h2, [role='heading']"),
// ].filter((e) =>
// AI_OVERVIEW_PATTERNS.some((pattern) => pattern.test(e.innerText)),
// );
// for (const aiText of aiTexts) {
// const block = getAiContainerParent(aiText);
// if (block) {
// return block;
// }
// }
return null;
};
const getThingsToKnow = () => {
const thingsToKnow = document.querySelectorAll('[data-maindata]:not([data-bkt])'); //TODO find a more reliable selector to hide things to know. This is at risk of hiding other non-AI features
for (const el of thingsToKnow) {
const block = getThingsToKnowContainer(el);
if (block) {
return block;
}
}
};
const isAiOverviewPaaTab = (el) => {
if (!el) return false;
if (!el || !el.matches("div.related-question-pair")) return false;
//pre-hydration tag on AI tabs
if (el.querySelector("[data-evn]")) return true;
//Post hydration AI tags:
if (el.querySelector('[data-subtree~="aimc"], [data-subtree="aimc"]')) {
return true;
}
if (el.querySelector('[data-subtree="aimfl,mfl"], [data-subtree="aimfl"]')) {
return true;
}
// Last-resort fallback once text has rendered
if (AI_OVERVIEW_PATTERNS.some((pattern) => pattern.test(el.innerText))) {
return true;
}
return false;
};
const observer = new MutationObserver(() => {
// each time there's a mutation in the document see if there's an ai overview to hide
const mainBody =
document.querySelector(DOM_SELECTORS.MAIN_BODY) ||
document.querySelector(DOM_SELECTORS.MAIN_BODY_MOBILE);
const aiOverview = getAiOverview(mainBody);
// Hide AI overview
if (aiOverview) aiOverview.style.display = CSS_VALUES.HIDDEN;
// Restore padding after header tabs
const headerTabs = document.querySelector(DOM_SELECTORS.HEADER_TABS);
if (headerTabs) headerTabs.style.paddingBottom = CSS_VALUES.HEADER_PADDING;
// For debugging
// console.log([...mainBody?.querySelectorAll('h1, h2')].map(e => { return { text: e.innerText, obj: e }}));
const mainElement = document.querySelector(DOM_SELECTORS.MAIN_ELEMENT);
if (mainElement) {
mainElement.style.marginTop = CSS_VALUES.MAIN_MARGIN;
}
// Remove entries in "People also ask" section if it contains "AI overview"
const peopleAlsoAskAiOverviews = [
...document.querySelectorAll(DOM_SELECTORS.PEOPLE_ALSO_ASK),
].filter(isAiOverviewPaaTab);
// peopleAlsoAskAiOverviews.forEach((el) => {
// el.parentElement.parentElement.style.display = CSS_VALUES.HIDDEN;
// });
// Hide Things to Know
const thingsToKnow = getThingsToKnow();
if (thingsToKnow) {
thingsToKnow.style.display = CSS_VALUES.HIDDEN;
}
// Hide AI Mode tab
const tabsList = document.querySelector(DOM_SELECTORS.TABS_LIST)?.children;
if (tabsList?.length) {
const aiModeTab = tabsList[0];
const text = aiModeTab.innerText.trim();
if (TAB_PATTERNS.AI_MODE.test(text)) {
aiModeTab.style.display = CSS_VALUES.HIDDEN;
}
}
});
observer.observe(document, {
childList: true,
subtree: true,
});