-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
239 lines (211 loc) · 8.76 KB
/
Copy pathdebug.js
File metadata and controls
239 lines (211 loc) · 8.76 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Get browserAPI using the polyfill
const browserAPI = typeof browser !== 'undefined' ? browser : chrome;
// Add setup sync storage button functionality
document.getElementById('setupTest').addEventListener('click', async () => {
try {
// Reset migration flag to force migration to happen
await browserAPI.storage.local.set({ migrationDone: false });
// Set up conflicting data in sync storage
await browserAPI.storage.sync.set({
userBangs: [
{
"d": "google.de",
"r": 500,
"s": "g",
"t": "g",
"u": "https://google.de/search?q={{{s}}}"
},
{
"d": "youtube.com",
"r": 500,
"s": "y",
"t": "y",
"u": "https://youtube.com/results?search_query={{{s}}}"
}
]
});
// Show success message
document.getElementById('storageDisplay').innerHTML = `
<div style="padding: 10px; background-color: #e8f5e9; border-radius: 4px; margin: 10px 0;">
<strong>Test data has been set up in sync storage!</strong>
<p>Now go to <a href="https://search.cogilabs.eu/" target="_blank">search.cogilabs.eu</a> to trigger the migration.</p>
<p>The migration should detect conflicts and show the resolution UI.</p>
</div>
`;
} catch (error) {
alert('Error setting up test: ' + error.message);
console.error('Setup error:', error);
}
});
// Add a button to set up local storage directly in the CogiSearch site
document.getElementById('setupLocal').addEventListener('click', async () => {
try {
// Find a tab with CogiSearch open
const tabs = await browserAPI.tabs.query({ url: "https://search.cogilabs.eu/*" });
if (tabs.length > 0) {
// We have CogiSearch open, inject the local bangs
console.log("Found CogiSearch tab, injecting local bangs");
// Define bang data outside the function to avoid serialization issues
const bangData = [
{
"d": "google.com",
"r": 500,
"s": "g",
"t": "g",
"u": "https://google.com/search?q={{{s}}}"
},
{
"d": "wikipedia.org",
"r": 500,
"s": "w",
"t": "w",
"u": "https://wikipedia.org/wiki/Special:Search?search={{{s}}}"
}
];
// Firefox-compatible script injection
const result = await browserAPI.scripting.executeScript({
target: { tabId: tabs[0].id },
func: (bangsArray) => {
// Set up localStorage with test data
localStorage.setItem('userBangs', JSON.stringify(bangsArray));
console.log('Local bangs set up in CogiSearch localStorage:', bangsArray);
return 'Local bangs set up in CogiSearch localStorage';
},
args: [bangData] // Pass bang data as an argument
});
console.log("Script execution result:", result);
document.getElementById('storageDisplay').innerHTML = `
<div style="padding: 10px; background-color: #e8f5e9; border-radius: 4px; margin: 10px 0;">
<strong>Local bangs set up in CogiSearch!</strong>
<p>Now click "Setup Test Data" to create conflicting sync storage bangs.</p>
</div>
`;
} else {
// No tab with CogiSearch open, suggest opening one
if (confirm('No CogiSearch tab found. Open one now?')) {
await browserAPI.tabs.create({ url: "https://search.cogilabs.eu/" });
document.getElementById('storageDisplay').innerHTML = `
<div style="padding: 10px; background-color: #fff3cd; border-radius: 4px; margin: 10px 0;">
<strong>CogiSearch opened in new tab.</strong>
<p>Please click this button again once the page has loaded.</p>
</div>
`;
}
}
} catch (error) {
alert('Error setting up local storage: ' + error.message);
console.error('Setup error:', error);
}
});
// Add clear storage button functionality
document.getElementById('clearStorage').addEventListener('click', async () => {
try {
// Clear extension storage (both local and sync) by setting to empty objects
await browserAPI.storage.local.remove(['migrationDone', 'userBangs']);
await browserAPI.storage.sync.remove(['userBangs']);
// Alternative approach if specific keys aren't known:
// Get all keys first, then remove them
const localData = await browserAPI.storage.local.get();
if (Object.keys(localData).length > 0) {
await browserAPI.storage.local.remove(Object.keys(localData));
}
const syncData = await browserAPI.storage.sync.get();
if (Object.keys(syncData).length > 0) {
await browserAPI.storage.sync.remove(Object.keys(syncData));
}
// Also clear localStorage userBangs
localStorage.removeItem('userBangs');
const display = document.getElementById('storageDisplay');
display.innerHTML = `
<div style="padding: 10px; background-color: #e8f5e9; border-radius: 4px; margin: 10px 0;">
Storage cleared successfully!
</div>
`;
alert('All storage cleared successfully');
} catch (error) {
alert('Error clearing storage: ' + error.message);
console.error('Clear storage error:', error);
}
});
// Update showStorage function to properly access website localStorage
document.getElementById('showStorage').addEventListener('click', async () => {
try {
// Get data from extension storage
const local = await browserAPI.storage.local.get();
const sync = await browserAPI.storage.sync.get();
// Get data from website localStorage using content script injection
let websiteLocalStorage = '(empty)';
// Find a CogiSearch tab
const tabs = await browserAPI.tabs.query({ url: "https://search.cogilabs.eu/*" });
if (tabs.length > 0) {
// Execute script in the CogiSearch tab to get localStorage
const result = await browserAPI.scripting.executeScript({
target: { tabId: tabs[0].id },
func: () => {
// Get the userBangs from the website's localStorage
const userBangs = localStorage.getItem('userBangs');
return userBangs;
}
});
// The result is an array of execution results
if (result && result[0] && result[0].result) {
try {
// Parse the JSON to display it nicely formatted
websiteLocalStorage = JSON.stringify(JSON.parse(result[0].result), null, 2);
} catch (e) {
websiteLocalStorage = result[0].result || '(empty)';
}
}
} else {
websiteLocalStorage = '(No CogiSearch tab open)';
}
// Get display element
const display = document.getElementById('storageDisplay');
// Format the data for display
display.innerHTML = `
<h3>Extension Local Storage</h3>
<pre>${JSON.stringify(local, null, 2) || '(empty)'}</pre>
<h3>Extension Sync Storage</h3>
<pre>${JSON.stringify(sync, null, 2) || '(empty)'}</pre>
<h3>Website localStorage (userBangs from search.cogilabs.eu)</h3>
<pre>${websiteLocalStorage}</pre>
`;
console.log('Current storage state:', { local, sync, websiteLocalStorage });
} catch (error) {
console.error('Error displaying storage:', error);
const display = document.getElementById('storageDisplay');
display.innerHTML = `
<div style="padding: 10px; background-color: #ffebee; border-radius: 4px; margin: 10px 0;">
Error displaying storage: ${error.message}
</div>
`;
alert('Error displaying storage: ' + error.message);
}
});
// Add button to open CogiSearch
document.getElementById('openSite').addEventListener('click', async () => {
try {
await browserAPI.tabs.create({ url: "https://search.cogilabs.eu/" });
} catch (error) {
alert('Error opening CogiSearch: ' + error.message);
}
});
// Add testing workflow guidance
document.getElementById('showGuide').addEventListener('click', () => {
document.getElementById('storageDisplay').innerHTML = `
<div style="padding: 15px; background-color: #f8f9fa; border-radius: 4px; margin: 10px 0; line-height: 1.5;">
<h3>How to Test Bang Conflict Resolution</h3>
<ol>
<li><strong>Clear all storage</strong> to start fresh</li>
<li><strong>Open CogiSearch</strong> in a new tab</li>
<li><strong>Set up local bangs</strong> on the CogiSearch website</li>
<li><strong>Set up sync storage</strong> with conflicting bangs</li>
<li><strong>Reload the CogiSearch tab</strong> to trigger migration</li>
<li>Observe the conflict resolution UI</li>
<li>Choose your preferred bangs</li>
<li>Check the result with "Show Current Storage"</li>
</ol>
<p>When migration is complete, the resolution UI should show up if there are conflicts.</p>
</div>
`;
});