|
1561 | 1561 | * @return {Promise<any>}
|
1562 | 1562 | */
|
1563 | 1563 | request(name, data = {}) {
|
1564 |
| - const id = name + ".response"; |
| 1564 | + const id = globalThis?.crypto?.randomUUID() || name + ".response"; |
1565 | 1565 | const message = new RequestMessage({
|
1566 | 1566 | context: this.messagingContext.context,
|
1567 | 1567 | featureName: this.messagingContext.featureName,
|
|
5140 | 5140 | let adLabelStrings = [];
|
5141 | 5141 | const parser = new DOMParser();
|
5142 | 5142 | let hiddenElements = /* @__PURE__ */ new WeakMap();
|
| 5143 | + let modifiedElements = /* @__PURE__ */ new WeakMap(); |
5143 | 5144 | let appliedRules = /* @__PURE__ */ new Set();
|
5144 | 5145 | let shouldInjectStyleTag = false;
|
5145 | 5146 | let mediaAndFormSelectors = "video,canvas,embed,object,audio,map,form,input,textarea,select,option,button";
|
5146 |
| - let hideTimeouts = [0, 100, 200, 300, 400, 500, 1e3, 1500, 2e3, 2500, 3e3, 5e3, 1e4]; |
5147 |
| - let unhideTimeouts = [750, 1500, 2250, 3e3, 4500, 6e3, 12e3]; |
| 5147 | + let hideTimeouts = [0, 100, 300, 500, 1e3, 2e3, 3e3]; |
| 5148 | + let unhideTimeouts = [1250, 2250, 3e3]; |
5148 | 5149 | let featureInstance;
|
5149 | 5150 | function collapseDomNode(element, rule, previousElement) {
|
5150 | 5151 | if (!element) {
|
5151 | 5152 | return;
|
5152 | 5153 | }
|
5153 | 5154 | const type = rule.type;
|
5154 | 5155 | const alreadyHidden = hiddenElements.has(element);
|
5155 |
| - if (alreadyHidden) { |
| 5156 | + const alreadyModified = modifiedElements.has(element) && modifiedElements.get(element) === rule.type; |
| 5157 | + if (alreadyHidden || alreadyModified) { |
5156 | 5158 | return;
|
5157 | 5159 | }
|
5158 |
| - featureInstance.addDebugFlag(); |
5159 | 5160 | switch (type) {
|
5160 | 5161 | case "hide":
|
5161 | 5162 | hideNode(element);
|
|
5174 | 5175 | appliedRules.add(rule);
|
5175 | 5176 | }
|
5176 | 5177 | break;
|
| 5178 | + case "modify-attr": |
| 5179 | + modifyAttribute(element, rule.values); |
| 5180 | + break; |
| 5181 | + case "modify-style": |
| 5182 | + modifyStyle(element, rule.values); |
| 5183 | + break; |
5177 | 5184 | }
|
5178 | 5185 | }
|
5179 | 5186 | function expandNonEmptyDomNode(element, rule) {
|
|
5206 | 5213 | element.style.setProperty("min-height", "0px", "important");
|
5207 | 5214 | element.style.setProperty("height", "0px", "important");
|
5208 | 5215 | element.hidden = true;
|
| 5216 | + featureInstance.addDebugFlag(); |
5209 | 5217 | }
|
5210 | 5218 | function unhideNode(element) {
|
5211 | 5219 | const cachedDisplayProperties = hiddenElements.get(element);
|
|
5241 | 5249 | }
|
5242 | 5250 | return false;
|
5243 | 5251 | }
|
| 5252 | + function modifyAttribute(element, values) { |
| 5253 | + values.forEach((item) => { |
| 5254 | + element.setAttribute(item.property, item.value); |
| 5255 | + }); |
| 5256 | + modifiedElements.set(element, "modify-attr"); |
| 5257 | + } |
| 5258 | + function modifyStyle(element, values) { |
| 5259 | + values.forEach((item) => { |
| 5260 | + element.style.setProperty(item.property, item.value, "important"); |
| 5261 | + }); |
| 5262 | + modifiedElements.set(element, "modify-style"); |
| 5263 | + } |
5244 | 5264 | function extractTimeoutRules(rules) {
|
5245 | 5265 | if (!shouldInjectStyleTag) {
|
5246 | 5266 | return rules;
|
|
5258 | 5278 | return timeoutRules;
|
5259 | 5279 | }
|
5260 | 5280 | function injectStyleTag(rules) {
|
5261 |
| - let styleTagContents = ""; |
| 5281 | + let selector = ""; |
5262 | 5282 | rules.forEach((rule, i) => {
|
5263 | 5283 | if (i !== rules.length - 1) {
|
5264 |
| - styleTagContents = styleTagContents.concat(rule.selector, ","); |
| 5284 | + selector = selector.concat(rule.selector, ","); |
5265 | 5285 | } else {
|
5266 |
| - styleTagContents = styleTagContents.concat(rule.selector); |
| 5286 | + selector = selector.concat(rule.selector); |
5267 | 5287 | }
|
5268 | 5288 | });
|
5269 |
| - styleTagContents = styleTagContents.concat("{display:none!important;min-height:0!important;height:0!important;}"); |
| 5289 | + const styleTagProperties = "{display:none!important;min-height:0!important;height:0!important;}"; |
| 5290 | + const styleTagContents = `${forgivingSelector(selector)} {${styleTagProperties}}`; |
5270 | 5291 | injectGlobalStyles(styleTagContents);
|
5271 | 5292 | }
|
5272 | 5293 | function hideAdNodes(rules) {
|
5273 | 5294 | const document2 = globalThis.document;
|
5274 | 5295 | rules.forEach((rule) => {
|
5275 |
| - const matchingElementArray = [...document2.querySelectorAll(rule.selector)]; |
| 5296 | + const selector = forgivingSelector(rule.selector); |
| 5297 | + const matchingElementArray = [...document2.querySelectorAll(selector)]; |
5276 | 5298 | matchingElementArray.forEach((element) => {
|
5277 | 5299 | collapseDomNode(element, rule);
|
5278 | 5300 | });
|
|
5281 | 5303 | function unhideLoadedAds() {
|
5282 | 5304 | const document2 = globalThis.document;
|
5283 | 5305 | appliedRules.forEach((rule) => {
|
5284 |
| - const matchingElementArray = [...document2.querySelectorAll(rule.selector)]; |
| 5306 | + const selector = forgivingSelector(rule.selector); |
| 5307 | + const matchingElementArray = [...document2.querySelectorAll(selector)]; |
5285 | 5308 | matchingElementArray.forEach((element) => {
|
5286 | 5309 | expandNonEmptyDomNode(element, rule);
|
5287 | 5310 | });
|
5288 | 5311 | });
|
5289 | 5312 | }
|
| 5313 | + function forgivingSelector(selector) { |
| 5314 | + return `:is(${selector})`; |
| 5315 | + } |
5290 | 5316 | class ElementHiding extends ContentFeature {
|
5291 | 5317 | init() {
|
5292 | 5318 | featureInstance = this;
|
5293 | 5319 | if (isBeingFramed()) {
|
5294 | 5320 | return;
|
5295 | 5321 | }
|
| 5322 | + let activeRules; |
5296 | 5323 | const globalRules = this.getFeatureSetting("rules");
|
5297 | 5324 | adLabelStrings = this.getFeatureSetting("adLabelStrings");
|
5298 | 5325 | shouldInjectStyleTag = this.getFeatureSetting("useStrictHideStyleTag");
|
|
5306 | 5333 | const overrideRules = activeDomainRules.filter((rule) => {
|
5307 | 5334 | return rule.type === "override";
|
5308 | 5335 | });
|
5309 |
| - let activeRules = activeDomainRules.concat(globalRules); |
| 5336 | + const disableDefault = activeDomainRules.some((rule) => { |
| 5337 | + return rule.type === "disable-default"; |
| 5338 | + }); |
| 5339 | + if (disableDefault) { |
| 5340 | + activeRules = activeDomainRules.filter((rule) => { |
| 5341 | + return rule.type !== "disable-default"; |
| 5342 | + }); |
| 5343 | + } else { |
| 5344 | + activeRules = activeDomainRules.concat(globalRules); |
| 5345 | + } |
5310 | 5346 | overrideRules.forEach((override) => {
|
5311 | 5347 | activeRules = activeRules.filter((rule) => {
|
5312 | 5348 | return rule.selector !== override.selector;
|
|
5339 | 5375 | */
|
5340 | 5376 | applyRules(rules) {
|
5341 | 5377 | const timeoutRules = extractTimeoutRules(rules);
|
| 5378 | + const clearCacheTimer = unhideTimeouts.concat(hideTimeouts).reduce((a, b) => Math.max(a, b), 0) + 100; |
5342 | 5379 | hideTimeouts.forEach((timeout) => {
|
5343 | 5380 | setTimeout(() => {
|
5344 | 5381 | hideAdNodes(timeoutRules);
|
|
5352 | 5389 | setTimeout(() => {
|
5353 | 5390 | appliedRules = /* @__PURE__ */ new Set();
|
5354 | 5391 | hiddenElements = /* @__PURE__ */ new WeakMap();
|
5355 |
| - }, 3100); |
| 5392 | + modifiedElements = /* @__PURE__ */ new WeakMap(); |
| 5393 | + }, clearCacheTimer); |
5356 | 5394 | }
|
5357 | 5395 | }
|
5358 | 5396 | class ExceptionHandler extends ContentFeature {
|
|
5387 | 5425 | ddg_feature_elementHiding: ElementHiding,
|
5388 | 5426 | ddg_feature_exceptionHandler: ExceptionHandler
|
5389 | 5427 | };
|
5390 |
| - function shouldRun() { |
5391 |
| - if (document instanceof HTMLDocument === false && (document instanceof XMLDocument === false || document.createElement("div") instanceof HTMLDivElement === false)) { |
5392 |
| - return false; |
5393 |
| - } |
5394 |
| - return true; |
5395 |
| - } |
5396 | 5428 | let initArgs = null;
|
5397 | 5429 | const updates = [];
|
5398 | 5430 | const features = [];
|
5399 | 5431 | const alwaysInitFeatures = /* @__PURE__ */ new Set(["cookie"]);
|
5400 | 5432 | const performanceMonitor = new PerformanceMonitor();
|
| 5433 | + const isHTMLDocument = document instanceof HTMLDocument || document instanceof XMLDocument && document.createElement("div") instanceof HTMLDivElement; |
5401 | 5434 | function load(args) {
|
5402 | 5435 | const mark = performanceMonitor.mark("load");
|
5403 |
| - if (!shouldRun()) { |
| 5436 | + if (!isHTMLDocument) { |
5404 | 5437 | return;
|
5405 | 5438 | }
|
5406 | 5439 | const featureNames = platformSupport["apple"];
|
|
5415 | 5448 | async function init(args) {
|
5416 | 5449 | const mark = performanceMonitor.mark("init");
|
5417 | 5450 | initArgs = args;
|
5418 |
| - if (!shouldRun()) { |
| 5451 | + if (!isHTMLDocument) { |
5419 | 5452 | return;
|
5420 | 5453 | }
|
5421 | 5454 | registerMessageSecret(args.messageSecret);
|
|
0 commit comments