-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-scripts.js
More file actions
263 lines (233 loc) · 7.57 KB
/
Copy pathbrowser-scripts.js
File metadata and controls
263 lines (233 loc) · 7.57 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Simple logger function for the application
*/
function log(level, message, context) {
// Create timestamp in ISO 8601 format
const timestamp = new Date().toISOString();
// Build log entry with standard required fields
const logEntry = {
timestamp,
level,
message,
...context,
};
// Output as JSON to appropriate console method based on level
console[level](JSON.stringify(logEntry));
}
/**
* Time interval in milliseconds for cycling through example states
*/
const EXAMPLE_CYCLE_INTERVAL_MS = 4000;
/**
* Track the current display state index
*/
let currentDisplayStateIndex = 0;
/**
* Collection of predefined currency states for the example cycle
*/
const currencyStates = [
{
// Default USD hourly wage worker
currencyCode: 'USD',
currencySymbol: '$',
incomeAmount: '7.25',
payFrequency: 'hourly',
productName: 'Taurinus X200',
productUrl: 'https://shop.libiquity.com/product/taurinus-x200',
priceValue: '445.00',
timeToEarn: '61h 23m',
},
{
// Medium USD yearly salary
currencyCode: 'USD',
currencySymbol: '$',
incomeAmount: '50,756.00',
payFrequency: 'yearly',
productName: 'Taurinus X200',
productUrl: 'https://shop.libiquity.com/product/taurinus-x200',
priceValue: '445.00',
timeToEarn: '18h 15m',
},
{
// GBP example
currencyCode: 'GBP',
currencySymbol: '£',
incomeAmount: '26,500.00',
payFrequency: 'yearly',
productName: 'Bulk crisps',
productUrl:
'https://www.britishfoodstoreonline.co.uk/products/McCoy%27s-Salted-Ridge-Cut-Potato-Crisps-30-x-50G.html',
priceValue: '24.99',
timeToEarn: '1h 58m',
},
{
// EUR example
currencyCode: 'EUR',
currencySymbol: '€',
incomeAmount: '9,61',
payFrequency: 'hourly',
productName: 'Kindle Paperwhite',
productUrl: 'http://amzn.to/2kEgPj2',
priceValue: '99,99',
timeToEarn: '10h 25m',
},
{
// MXN example
currencyCode: 'MXN',
currencySymbol: '$',
incomeAmount: '31.30',
payFrequency: 'hourly',
productName: 'Licuadora Oster',
productUrl: 'http://bit.ly/2ldKpPN',
priceValue: '1,149.00',
timeToEarn: '36h 43m',
},
];
/**
* Calculate the next state index, cycling through available states
*/
function calculateNextStateIndex(currentIndex, totalStates) {
return (currentIndex + 1) % totalStates;
}
/**
* Determines the next currency state
*/
function getNextState() {
const nextIndex = calculateNextStateIndex(currentDisplayStateIndex, currencyStates.length);
currentDisplayStateIndex = nextIndex;
return currencyStates[nextIndex];
}
/**
* Validates if a string is a valid HTTP or HTTPS URL
*/
function isValidHttpUrl(url) {
try {
const parsed = new URL(url);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}
/**
* Updates the example display to show different currency and income scenarios.
*/
function shiftExample() {
// Get DOM container
const container = document.body;
if (!container) {
log('error', 'Required DOM body not found', {
component: 'shiftExample',
});
throw new Error('Initialization failed: document.body not found');
}
// Compute next state
const nextState = getNextState();
// Apply state to DOM
applyState(nextState, container);
}
/**
* Applies a currency state to DOM elements in a container
*/
function applyState(state, container) {
// Find all required elements within the container
const currencyCodeElement = container.querySelector('#currency-code');
const currencySymbolElement = container.querySelector('#currency-symbol');
const incomeAmountElement = container.querySelector('#income-amount');
const payFrequencyElement = container.querySelector('#pay-frequency');
const exampleProductElement = container.querySelector('#example-product');
const examplePriceElement = container.querySelector('#example-price');
// Check if all required elements exist
const elementsToCheck = [
{ element: currencyCodeElement, id: 'currency-code' },
{ element: currencySymbolElement, id: 'currency-symbol' },
{ element: incomeAmountElement, id: 'income-amount' },
{ element: payFrequencyElement, id: 'pay-frequency' },
{ element: exampleProductElement, id: 'example-product' },
{ element: examplePriceElement, id: 'example-price' },
];
const missingElements = elementsToCheck.filter((item) => !item.element).map((item) => item.id);
if (missingElements.length > 0) {
log('error', 'Required child elements not found in container', {
component: 'applyState',
containerId: container.id || '[unnamed]',
missingElements,
});
throw new Error('Container missing required child elements');
}
// Update basic text fields
currencyCodeElement.textContent = state.currencyCode;
currencySymbolElement.textContent = state.currencySymbol;
incomeAmountElement.textContent = state.incomeAmount;
payFrequencyElement.textContent = state.payFrequency;
// Clear any existing content
exampleProductElement.innerHTML = '';
examplePriceElement.innerHTML = '';
// Create product element programmatically
const productLink = document.createElement('a');
// URL Validation
if (isValidHttpUrl(state.productUrl)) {
productLink.href = state.productUrl;
} else {
productLink.href = '#';
log('error', `Invalid URL skipped: ${state.productUrl}`, { component: 'applyState' });
}
productLink.target = '_blank';
productLink.textContent = state.productName;
exampleProductElement.appendChild(productLink);
// Add a colon after the product if not MXN (MXN example doesn't have one)
if (state.currencyCode !== 'MXN') {
exampleProductElement.appendChild(document.createTextNode(': '));
}
// Create price element programmatically
const priceContainer = document.createElement('span');
priceContainer.className = 'example-money';
// Different formatting for EUR vs other currencies
if (state.currencyCode === 'EUR') {
priceContainer.textContent = state.priceValue + ' ';
const currencySpan = document.createElement('span');
currencySpan.textContent = 'EUR';
priceContainer.appendChild(currencySpan);
} else {
const currencySpan = document.createElement('span');
currencySpan.textContent = state.currencySymbol;
priceContainer.appendChild(currencySpan);
priceContainer.appendChild(document.createTextNode(state.priceValue));
}
// Add time to earn
priceContainer.appendChild(document.createTextNode(' (' + state.timeToEarn + ')'));
examplePriceElement.appendChild(priceContainer);
}
/**
* Updates the copyright element in the DOM with the current year
*/
function applyCopyrightText() {
const copyrightElement = document.getElementById('copyright');
if (copyrightElement) {
// Clear any existing content
copyrightElement.innerHTML = '';
// Add copyright symbol and year text
const year = new Date().getFullYear();
copyrightElement.appendChild(document.createTextNode('Copyright © ' + year + ' '));
// Create and append the link to Phaedrus
const link = document.createElement('a');
link.href = 'https://www.phaedrus.io';
link.target = '_blank';
link.textContent = 'Phaedrus';
copyrightElement.appendChild(link);
}
}
/**
* Starts the interval timer that cycles through example currency displays
*/
function startExampleInterval() {
setInterval(shiftExample, EXAMPLE_CYCLE_INTERVAL_MS);
}
/**
* Initializes the application
*/
function initializeApplication() {
startExampleInterval();
applyCopyrightText();
}
// Expose to global scope
window.initializeApplication = initializeApplication;