-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
82 lines (73 loc) · 2.4 KB
/
Copy pathbackground.js
File metadata and controls
82 lines (73 loc) · 2.4 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
// Open the onboarding page on first install (not on updates)
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.tabs.create({ url: 'welcome.html' });
}
});
// Listen for extension icon click
chrome.action.onClicked.addListener((tab) => {
// First inject the analyzer.js script
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
files: ['src/analyzer-bundle.js']
},
() => {
// After analyzer.js is injected, run the analysis (async)
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: async () => await window.pageAnalyzer()
},
(results) => {
if (results && results[0] && results[0].result) {
const analysisResults = results[0].result;
// Show notification with results
showNotification(analysisResults, tab.url);
// Save to history
saveToHistory(tab.url, tab.title || tab.url, analysisResults);
} else {
// Show error notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon48.png',
title: 'Analysis Failed',
message: 'Unable to analyze the current page. Please try again.',
priority: 1
});
}
}
);
}
);
});
// Show notification with analysis results
function showNotification(results, url) {
const { renderType, confidence, indicators } = results;
// Create notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon48.png',
title: `${renderType} (${confidence}% confidence)`,
message: `Key indicators: ${indicators.slice(0, 2).join(', ')}${indicators.length > 2 ? '...' : ''}`,
priority: 1
});
}
// Save analysis to history
function saveToHistory(url, title, results) {
chrome.storage.local.get(['analysisHistory'], (data) => {
const history = data.analysisHistory || [];
// Add new entry (limit to 10 entries)
const newEntry = {
url: url,
title: title,
timestamp: Date.now(),
results: results
};
// Add to beginning of array and limit to 10 entries
history.unshift(newEntry);
if (history.length > 10) history.pop();
// Save back to storage
chrome.storage.local.set({ analysisHistory: history });
});
}