-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
180 lines (156 loc) · 5.27 KB
/
Copy pathcontent.js
File metadata and controls
180 lines (156 loc) · 5.27 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
// Content script for Ally extension
// This script runs on web pages and modifies the DOM based on the selected profile
let currentProfile = null;
let aiAgent = null;
// Initialize content script
(async () => {
// Load active profile from storage
const result = await chrome.storage.local.get(['activeProfile']);
if (result.activeProfile) {
currentProfile = result.activeProfile;
applyProfile(result.activeProfile);
}
})();
// Listen for profile changes from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'profileChanged' || message.action === 'applyProfile') {
currentProfile = message.profile;
if (message.profile) {
applyProfile(message.profile);
} else {
removeProfile();
}
}
});
// Apply profile modifications to the page
async function applyProfile(profile) {
console.log('Applying profile:', profile);
// Initialize AI agent if not already done
if (!aiAgent) {
aiAgent = new AIAgent();
}
// Get AI-suggested DOM modifications for this profile
const modifications = await aiAgent.getModifications(profile, document);
// Apply modifications
if (modifications && modifications.length > 0) {
modifications.forEach(mod => {
applyModification(mod);
});
} else {
// Fallback to basic modifications if AI is not available
applyBasicModifications(profile);
}
}
// Remove profile modifications
function removeProfile() {
console.log('Removing profile modifications');
// Remove any added classes, styles, or elements
document.body.classList.remove('ally-active');
document.body.classList.remove('ally-visually-challenged');
document.body.classList.remove('ally-hearing-impaired');
document.body.classList.remove('ally-motor-impaired');
document.body.classList.remove('ally-autistic');
// Remove any injected styles
const styleElement = document.getElementById('ally-styles');
if (styleElement) {
styleElement.remove();
}
}
// Apply a single modification
function applyModification(modification) {
// This will be implemented based on the AI agent's response format
// For now, it's a placeholder structure
console.log('Applying modification:', modification);
}
// Basic modifications as fallback (before AI integration)
function applyBasicModifications(profile) {
document.body.classList.add('ally-active', `ally-${profile}`);
// Create and inject basic styles
let styleElement = document.getElementById('ally-styles');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.id = 'ally-styles';
document.head.appendChild(styleElement);
}
let styles = '';
switch (profile) {
case 'visually-challenged':
styles = `
.ally-visually-challenged * {
font-size: 1.2em !important;
}
.ally-visually-challenged {
filter: contrast(1.2) !important;
}
`;
break;
case 'hearing-impaired':
// Will be handled by AI agent for captions/transcripts
break;
case 'motor-impaired':
styles = `
.ally-motor-impaired button,
.ally-motor-impaired a,
.ally-motor-impaired input {
min-height: 44px !important;
min-width: 44px !important;
padding: 12px !important;
}
`;
break;
case 'autistic':
styles = `
.ally-autistic * {
animation: none !important;
transition: none !important;
}
.ally-autistic {
filter: brightness(0.9) !important;
}
`;
break;
}
if (styles) {
styleElement.textContent = styles;
}
}
// AI Agent class for getting DOM modifications
class AIAgent {
constructor() {
this.apiKey = null; // Will be set later
this.apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';
}
async getModifications(profile, document) {
// This will be implemented to call Gemini API
// For now, return null to use fallback modifications
// TODO: Implement Gemini API integration
try {
// Analyze the current page structure
const pageAnalysis = this.analyzePage(document);
// Call AI API (will be implemented)
// const response = await this.callGeminiAPI(profile, pageAnalysis);
// Return modifications
return null; // Placeholder
} catch (error) {
console.error('Error getting AI modifications:', error);
return null; // Fallback to basic modifications
}
}
analyzePage(document) {
// Analyze the page structure for AI context
return {
title: document.title,
headings: Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6')).map(h => h.textContent),
links: Array.from(document.querySelectorAll('a')).length,
buttons: Array.from(document.querySelectorAll('button')).length,
images: Array.from(document.querySelectorAll('img')).length,
videos: Array.from(document.querySelectorAll('video')).length,
};
}
async callGeminiAPI(profile, pageAnalysis) {
// TODO: Implement Gemini API call
// This will send the profile and page analysis to Gemini
// and receive back specific DOM modification instructions
throw new Error('Gemini API integration not yet implemented');
}
}