109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
(function (root) {
|
|
'use strict';
|
|
|
|
var S = root.AutoMirrorShared;
|
|
|
|
function findBlockedMarker(rule) {
|
|
return S.findBlockedMarkerInPage(
|
|
rule,
|
|
document.title || '',
|
|
document.body ? document.body.innerText : ''
|
|
);
|
|
}
|
|
|
|
function sendDebug(event, data, level) {
|
|
try {
|
|
chrome.runtime.sendMessage({
|
|
type: 'DEBUG_LOG',
|
|
level: level || 'debug',
|
|
event: event,
|
|
data: data || {}
|
|
});
|
|
} catch (e) {
|
|
// Diagnostics must never break the page.
|
|
}
|
|
}
|
|
|
|
function reportBlocked(rule, marker) {
|
|
chrome.runtime.sendMessage({
|
|
type: 'BLOCKED_PAGE_DETECTED',
|
|
url: location.href,
|
|
marker: marker || '',
|
|
rule: rule && (rule.name || rule.id) || '',
|
|
title: document.title || ''
|
|
});
|
|
}
|
|
|
|
function reportOk(rule) {
|
|
chrome.runtime.sendMessage({
|
|
type: 'PAGE_OK',
|
|
url: location.href,
|
|
rule: rule && (rule.name || rule.id) || '',
|
|
title: document.title || ''
|
|
});
|
|
}
|
|
|
|
function run() {
|
|
chrome.storage.local.get({ enabled: true, rules: [] }, function (items) {
|
|
if (items.enabled === false || !Array.isArray(items.rules)) return;
|
|
|
|
var rule = S.findRuleForHost(items.rules, location.hostname);
|
|
if (!rule) return;
|
|
|
|
sendDebug('content.ruleMatched', {
|
|
url: location.href,
|
|
host: location.hostname,
|
|
rule: rule.name || rule.id || 'unknown',
|
|
mode: rule.mode || 'step',
|
|
strategy: rule.strategy || 'number'
|
|
});
|
|
|
|
var delays = [0, 150, 600, 1800, 4200];
|
|
var reported = false;
|
|
|
|
delays.forEach(function (delay, index) {
|
|
root.setTimeout(function () {
|
|
if (reported) return;
|
|
|
|
var marker = findBlockedMarker(rule);
|
|
|
|
if (marker) {
|
|
reported = true;
|
|
sendDebug('content.blockedMarkerFound', {
|
|
url: location.href,
|
|
host: location.hostname,
|
|
rule: rule.name || rule.id || 'unknown',
|
|
marker: marker,
|
|
checkDelayMs: delay,
|
|
title: document.title || ''
|
|
}, 'warn');
|
|
reportBlocked(rule, marker);
|
|
return;
|
|
}
|
|
|
|
sendDebug('content.markerCheckClean', {
|
|
url: location.href,
|
|
host: location.hostname,
|
|
rule: rule.name || rule.id || 'unknown',
|
|
checkDelayMs: delay,
|
|
checkIndex: index + 1,
|
|
totalChecks: delays.length,
|
|
title: document.title || ''
|
|
});
|
|
|
|
if (index === delays.length - 1) {
|
|
reported = true;
|
|
reportOk(rule);
|
|
}
|
|
}, delay);
|
|
});
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', run);
|
|
} else {
|
|
run();
|
|
}
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this);
|