Skip to content

Commit

Permalink
update background.js
Browse files Browse the repository at this point in the history
Created a dedicated `ContentAnalyzer` class to handle content analysis
  • Loading branch information
RootUp authored Dec 13, 2024
1 parent 0ed7a70 commit c71071b
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions SmuggleShield/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,146 @@ const memoizedAnalyzeURL = memoize((url) => {
const isSuspicious = checkSuspiciousURL(url);
return {isSuspicious, timestamp: Date.now()};
}, (url) => url);

class ContentAnalyzer {
constructor(config) {
this.config = config;
this.analysisQueue = new Set();
this.isProcessing = false;
this.lastAnalysis = 0;
this.minAnalysisInterval = 300; // ms between analyses
}

queueForAnalysis(element) {
this.analysisQueue.add(element);
this.scheduleProcessing();
}

scheduleProcessing() {
if (this.isProcessing) return;

const timeSinceLastAnalysis = Date.now() - this.lastAnalysis;
const delay = Math.max(0, this.minAnalysisInterval - timeSinceLastAnalysis);

setTimeout(() => this.processQueue(), delay);
}

async processQueue() {
if (this.isProcessing || this.analysisQueue.size === 0) return;

this.isProcessing = true;
this.lastAnalysis = Date.now();

try {
const elements = Array.from(this.analysisQueue);
this.analysisQueue.clear();

for (const element of elements) {
if (element.isConnected) {
await this.analyzeElement(element);
}
}
} catch (error) {
console.error('Analysis error:', error);
} finally {
this.isProcessing = false;

if (this.analysisQueue.size > 0) {
this.scheduleProcessing();
}
}
}

async analyzeElement(element) {
if (element.textContent.length < 50) return;

const htmlContent = element.outerHTML;
const cacheKey = this.getCacheKey(htmlContent);

const cachedResult = urlCache.get(cacheKey);
if (cachedResult) {
if (cachedResult.isSuspicious) {
this.handleSuspiciousContent(element, cachedResult.patterns);
}
return;
}

const result = await this.analyzeContent(htmlContent);
urlCache.set(cacheKey, result);

if (result.isSuspicious) {
this.handleSuspiciousContent(element, result.patterns);
}
}

getCacheKey(content) {
let hash = 0;
for (let i = 0; i < content.length; i++) {
hash = ((hash << 5) - hash) + content.charCodeAt(i);
hash = hash & hash;
}
return `${hash}_${content.length}`;
}

async analyzeContent(content) {
const patterns = [];
let score = 0;

for (const pattern of this.config.suspiciousPatterns) {
if (pattern.test(content)) {
patterns.push(pattern.source);
score += pattern.weight || 1;

if (score >= this.config.blockThreshold) {
break;
}
}
}

return {
isSuspicious: score >= this.config.blockThreshold,
patterns,
timestamp: Date.now()
};
}

handleSuspiciousContent(element, patterns) {
chrome.runtime.sendMessage({
action: "logWarning",
message: `Suspicious content detected`,
patterns: patterns,
url: window.location.href
});

if (element.parentNode) {
element.parentNode.removeChild(element);
}
}
}

const contentAnalyzer = new ContentAnalyzer(config);

function setupObserver() {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
mutation.addedNodes.forEach(node => {
if (node instanceof HTMLElement) {
contentAnalyzer.queueForAnalysis(node);
}
});

if (mutation.type === 'attributes' && mutation.target instanceof HTMLElement) {
contentAnalyzer.queueForAnalysis(mutation.target);
}
}
});

observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['src', 'href', 'data']
});

contentAnalyzer.queueForAnalysis(document.documentElement);
}

0 comments on commit c71071b

Please sign in to comment.