-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
937 lines (827 loc) · 40.1 KB
/
content.js
File metadata and controls
937 lines (827 loc) · 40.1 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
(function() { // Wrap the entire script in an IIFE to control execution.
/**
* @fileoverview Content script for Ops Toolshed.
* Handles page interactions, logo replacement, reminders (Meta, IAS, Custom),
* and automated form filling on Prisma and Aura pages.
*/
chrome.storage.local.get('timeBombActive', (data) => {
if (data.timeBombActive) {
console.log('Ops Toolshed features disabled due to time bomb.');
return; // Do not initialize anything if the time bomb is active.
}
// If not active, run the main script logic.
initializeContentScript();
});
/**
* Initializes the content script logic.
*/
function initializeContentScript() {
console.log("[ContentScript Prisma] Script Injected on URL:", window.location.href, "at", new Date().toLocaleTimeString());
// Global variables for custom reminders
let activeCustomReminders = [];
let shownCustomReminderIds = new Set();
let mediaMixAutomated = false;
let budgetTypeAutomated = false;
// Utility to escape HTML for display (used by custom reminder popup)
/**
* Escapes HTML characters in a string to prevent XSS.
* @param {string} str - The string to escape.
* @returns {string} The escaped HTML string.
*/
function escapeHTML(str) {
if (str === null || str === undefined) return '';
const div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
/**
* Recursively searches for an element matching the selector, piercing through shadow DOMs.
* @param {string} selector - The CSS selector to search for.
* @param {Element|ShadowRoot} [root=document] - The root element to start the search from.
* @returns {Element|null} The first matching element found, or null.
*/
function queryShadowDom(selector, root = document) {
const found = root.querySelector(selector);
if (found) return found;
const allElements = root.querySelectorAll('*');
for (const element of allElements) {
if (element.shadowRoot) {
const foundInShadow = queryShadowDom(selector, element.shadowRoot);
if (foundInShadow) return foundInShadow;
}
}
return null;
}
/**
* Waits for an element to appear in the DOM.
* @param {string} selector - The CSS selector of the element to wait for.
* @param {number} [timeout=2000] - The maximum time to wait in milliseconds.
* @returns {Promise<Element>} A promise that resolves with the found element.
*/
function waitForElement(selector, timeout = 2000) {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const element = document.querySelector(selector);
if (element) {
clearInterval(interval);
clearTimeout(timer);
resolve(element);
}
}, 100);
const timer = setTimeout(() => {
clearInterval(interval);
reject(new Error(`Element '${selector}' not found within ${timeout}ms`));
}, timeout);
});
}
/**
* Waits for an element to disappear from the DOM.
* @param {string} selector - The CSS selector of the element to wait for.
* @param {number} [timeout=2000] - The maximum time to wait in milliseconds.
* @returns {Promise<void>} A promise that resolves when the element disappears.
*/
function waitForElementToDisappear(selector, timeout = 2000) {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const element = document.querySelector(selector);
if (!element) {
clearInterval(interval);
clearTimeout(timer);
resolve();
}
}, 100);
const timer = setTimeout(() => {
clearInterval(interval);
reject(new Error(`Element '${selector}' did not disappear within ${timeout}ms`));
}, timeout);
});
}
/**
* Replaces the Prisma logo with a custom logo (the extension icon).
* Saves the original SVG content to allow restoration.
*/
function replaceLogo() {
// Use a more robust selector by finding a unique path within the SVG,
// and use queryShadowDom to search inside shadow DOM trees.
const uniquePath = queryShadowDom('path[d="M9.23616 0C4.13364 0 0 3.78471 0 8.455C0 13.1253 4.13364 16.91 9.23616 16.91"]');
const specificSvg = uniquePath ? uniquePath.closest('svg') : null;
const logoContainer = specificSvg ? specificSvg.parentElement : null;
if (logoContainer) {
// Check if custom logo already exists by checking for our specific class within the container
if (logoContainer.querySelector('.custom-prisma-logo')) {
return; // Already replaced
}
// Store original content (the SVG itself) if not already stored
if (!logoContainer.dataset.originalSvgContent && specificSvg) {
logoContainer.dataset.originalSvgContent = specificSvg.outerHTML;
}
// Remove the original SVG
if (specificSvg) {
specificSvg.remove();
}
const newLogoImg = document.createElement('img');
newLogoImg.src = chrome.runtime.getURL('icon.png');
newLogoImg.style.width = '32px'; // Or use dimensions from the original SVG if desired
newLogoImg.style.height = '28px'; // Match height of original SVG container
newLogoImg.style.objectFit = 'contain';
newLogoImg.className = 'custom-prisma-logo';
logoContainer.appendChild(newLogoImg);
// console.log("[ContentScript Prisma] Prisma logo SVG replaced with custom image.");
}
}
/**
* Restores the original Prisma logo from the stored SVG content.
*/
function restoreOriginalLogo() {
const customLogoImg = document.querySelector('i.logo > img.custom-prisma-logo');
if (customLogoImg) {
const logoContainer = customLogoImg.parentElement; // The <i> tag
if (logoContainer && logoContainer.dataset.originalSvgContent) {
customLogoImg.remove(); // Remove the custom image
// Prepend original SVG. Ensure it's not added multiple times if logic re-runs.
if (!logoContainer.querySelector('svg[width="20"][height="28"]')) {
logoContainer.innerHTML = logoContainer.dataset.originalSvgContent + logoContainer.innerHTML;
}
// console.log("[ContentScript Prisma] Original Prisma SVG logo restored from stored content.");
} else if (logoContainer) {
// Fallback if original content wasn't stored: just remove the custom logo
customLogoImg.remove();
// console.log("[ContentScript Prisma] Custom logo image removed (no stored original SVG). Page may need refresh for original logo.");
}
}
}
/**
* Checks the user preference and either replaces or restores the logo.
*/
function checkAndReplaceLogo() {
// Gracefully handle cases where the extension context is invalidated.
if (!chrome.runtime || !chrome.runtime.id) {
// The extension context has been invalidated, so we can't use chrome.storage.
// This can happen if the extension is reloaded or updated.
// console.log("Extension context invalidated. Skipping logo replacement check.");
return;
}
chrome.storage.sync.get('logoReplaceEnabled', function(data) {
if (chrome.runtime.lastError) {
console.error(`Error getting logoReplaceEnabled setting: ${chrome.runtime.lastError.message}`);
return;
}
// In settings.js, the default is true if the value is undefined.
// So we should replace the logo unless the setting is explicitly false.
if (data.logoReplaceEnabled !== false) {
replaceLogo();
} else {
restoreOriginalLogo();
}
});
}
// addReminderStyles function is now removed as styles are in style.css
// Ensure style.css is listed in manifest.json's content_scripts css array.
let metaReminderDismissed = false;
let iasReminderDismissed = false;
let metaCheckInProgress = false;
// --- New Reminder Logic ---
/**
* Determines whether a reminder should be shown based on frequency and last shown time.
* @param {string} storageKey - The key in local storage for the last shown timestamp.
* @param {string} frequency - The frequency setting ('daily', 'weekly', 'monthly', 'once').
* @param {function(boolean): void} callback - The callback receiving the decision (true to show).
*/
function shouldShowReminder(storageKey, frequency, callback) {
chrome.storage.local.get([storageKey], (data) => {
const lastShownTimestamp = data[storageKey];
if (!lastShownTimestamp) {
callback(true); // Never shown, so show it.
return;
}
const now = new Date();
const lastShown = new Date(lastShownTimestamp);
let show = false;
switch (frequency) {
case 'daily':
if (now.toDateString() !== lastShown.toDateString()) show = true;
break;
case 'weekly':
const oneWeek = 7 * 24 * 60 * 60 * 1000;
if (now.getTime() - lastShown.getTime() > oneWeek) show = true;
break;
case 'monthly':
if (now.getMonth() !== lastShown.getMonth() || now.getFullYear() !== lastShown.getFullYear()) show = true;
break;
case 'once':
// Handled by the initial check for `lastShownTimestamp`. If we get here, it's already been shown.
show = false;
break;
default: // Default to daily
if (now.toDateString() !== lastShown.toDateString()) show = true;
break;
}
callback(show);
});
}
/**
* Updates the last shown timestamp for a reminder in local storage.
* @param {string} storageKey - The key in local storage.
*/
function setReminderShown(storageKey) {
chrome.storage.local.set({ [storageKey]: Date.now() });
}
/**
* Creates and displays a standardized reminder popup.
* @param {Object} params - The parameters for the popup.
* @param {string} params.popupId - The unique ID for the popup element.
* @param {Object} params.content - Content object containing title, message, and list items.
* @param {string} params.content.title - Title of the reminder.
* @param {string} params.content.message - Message body of the reminder.
* @param {string[]} params.content.list - List of items to display.
* @param {number} params.countdownSeconds - Seconds before the close button is enabled.
* @param {string} params.storageKey - The storage key to update when dismissed.
*/
function createPrismaReminderPopup({ popupId, content, countdownSeconds, storageKey }) {
if (document.getElementById(popupId)) return;
const overlay = document.createElement('div');
overlay.className = 'reminder-overlay';
overlay.id = `${popupId}-overlay`;
document.body.appendChild(overlay);
const popup = document.createElement('div');
popup.id = popupId;
const h3 = document.createElement('h3');
h3.textContent = content.title;
popup.appendChild(h3);
const p = document.createElement('p');
p.textContent = content.message;
popup.appendChild(p);
const ul = document.createElement('ul');
content.list.forEach(itemText => {
const li = document.createElement('li');
li.textContent = itemText;
ul.appendChild(li);
});
popup.appendChild(ul);
const closeButton = document.createElement('button');
closeButton.id = `${popupId}-close`;
closeButton.className = 'reminder-close-button';
closeButton.textContent = 'Got it!';
popup.appendChild(closeButton);
document.body.appendChild(popup);
console.log(`[ContentScript Prisma] ${content.title} popup CREATED.`);
let countdownInterval;
const cleanupPopup = () => {
popup.remove();
overlay.remove();
clearInterval(countdownInterval);
setReminderShown(storageKey);
if (popupId === 'meta-reminder-popup') {
metaReminderDismissed = true;
metaCheckInProgress = false; // Reset the check flag on dismissal
}
if (popupId === 'ias-reminder-popup') iasReminderDismissed = true;
};
if (countdownSeconds > 0) {
closeButton.disabled = true;
let secondsLeft = countdownSeconds;
closeButton.textContent = `Got it! (${secondsLeft}s)`;
countdownInterval = setInterval(() => {
secondsLeft--;
if (secondsLeft > 0) {
closeButton.textContent = `Got it! (${secondsLeft}s)`;
} else {
clearInterval(countdownInterval);
closeButton.textContent = 'Got it!';
closeButton.disabled = false;
}
}, 1000);
}
closeButton.addEventListener('click', cleanupPopup);
}
/**
* Checks if conditions for the Meta Reconciliation Reminder are met and shows it if necessary.
*/
function checkForMetaConditions() {
if (metaReminderDismissed || metaCheckInProgress) return;
const currentUrl = window.location.href;
if (!currentUrl.includes('groupmuk-prisma.mediaocean.com/') || !currentUrl.includes('actualize')) return;
if (!chrome.runtime || !chrome.runtime.id) return;
chrome.storage.sync.get(['metaReminderEnabled', 'prismaReminderFrequency', 'prismaCountdownDuration'], (settings) => {
if (chrome.runtime.lastError || settings.metaReminderEnabled === false) return;
const frequency = settings.prismaReminderFrequency || 'daily';
shouldShowReminder('metaReminderLastShown', frequency, (show) => {
if (!show) return;
metaCheckInProgress = true;
let attempts = 0;
const maxAttempts = 15;
const intervalId = setInterval(() => {
const pageText = document.body.textContent || "";
const conditionsMet = pageText.includes('000770') && pageText.includes('Redistribute all');
if (conditionsMet) {
clearInterval(intervalId);
// metaCheckInProgress remains true until the user dismisses the popup.
if (!document.getElementById('meta-reminder-popup')) {
createPrismaReminderPopup({
popupId: 'meta-reminder-popup',
content: {
title: '⚠️ Meta Reconciliation Reminder ⚠️',
message: 'When reconciling Meta, please:',
list: ["Actualise to the 'Supplier' option", "Self-accept the IO", "Push through on trafficking tab to Meta", "Verify success of the push, every time", "Do not just leave the page!"]
},
countdownSeconds: parseInt(settings.prismaCountdownDuration, 10) || 0,
storageKey: 'metaReminderLastShown'
});
}
} else if (attempts >= maxAttempts) {
clearInterval(intervalId);
metaCheckInProgress = false; // Reset only on timeout
}
attempts++;
}, 2000);
});
});
}
/**
* Checks if conditions for the IAS Booking Reminder are met and shows it if necessary.
*/
function checkForIASConditions() {
if (iasReminderDismissed) return;
if (!chrome.runtime || !chrome.runtime.id) return;
chrome.storage.sync.get(['iasReminderEnabled', 'prismaReminderFrequency', 'prismaCountdownDuration'], (settings) => {
if (chrome.runtime.lastError || settings.iasReminderEnabled === false) return;
const pageText = document.body.innerText;
if (pageText.includes('001148') && pageText.includes('Flat') && pageText.includes('Unit type')) {
const frequency = settings.prismaReminderFrequency || 'daily';
shouldShowReminder('iasReminderLastShown', frequency, (show) => {
if (show && !document.getElementById('ias-reminder-popup')) {
createPrismaReminderPopup({
popupId: 'ias-reminder-popup',
content: {
title: '⚠️ IAS Booking Reminder ⚠️',
message: 'Please ensure you book as CPM',
list: ['With correct rate for media type', 'Check the plan', 'Ensure what is planned is what goes live']
},
countdownSeconds: parseInt(settings.prismaCountdownDuration, 10) || 0,
storageKey: 'iasReminderLastShown'
});
}
});
}
});
}
let currentUrlForDismissFlags = window.location.href;
setInterval(() => {
if (currentUrlForDismissFlags !== window.location.href) {
console.log("[ContentScript Prisma] URL changed, reminder dismissal flags reset.");
metaReminderDismissed = false;
iasReminderDismissed = false;
shownCustomReminderIds.clear(); // Reset shown custom reminders on URL change
mediaMixAutomated = false;
budgetTypeAutomated = false;
currentUrlForDismissFlags = window.location.href;
// Potentially re-fetch or re-check custom reminders if needed immediately on SPA navigation
// For now, MutationObserver and initial load handle most cases.
// checkCustomReminders(); // Optional: check immediately on navigation
}
}, 500);
// --- Custom Reminder Functions ---
/**
* Fetches active custom reminders from storage.
*/
function fetchCustomReminders() {
if (!chrome.runtime || !chrome.runtime.id) return; // Context guard
chrome.storage.sync.get({customReminders: []}, function(data) {
if (chrome.runtime.lastError) {
console.error("[ContentScript Prisma] Error fetching custom reminders:", chrome.runtime.lastError);
activeCustomReminders = [];
return;
}
activeCustomReminders = data.customReminders.filter(r => r.enabled);
// console.log("[ContentScript Prisma] Fetched active custom reminders:", activeCustomReminders);
// Resetting shownCustomReminderIds here might be too broad if only one reminder setting changed.
// However, the message listener for "customRemindersUpdated" already clears it, which is often sufficient.
});
}
/**
* Converts a wildcard pattern to a regular expression.
* @param {string} pattern - The wildcard pattern (e.g., "*example*").
* @returns {RegExp} The equivalent regular expression.
*/
function wildcardToRegex(pattern) {
// Escape regex special chars
let escapedPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
// If no wildcard '*' is present in the original pattern, assume it means 'contains'
// So, add '.*' to the beginning and end of the escaped pattern.
// Otherwise, just convert user-defined '*' to '.*'
if (!pattern.includes('*')) {
escapedPattern = '.*' + escapedPattern + '.*';
} else {
// Convert user-defined '*' to '.*'
escapedPattern = escapedPattern.replace(/\*/g, '.*');
}
// Always ensure the regex is case-insensitive for URL matching, and still use ^$ to match the whole URL against the pattern
return new RegExp('^' + escapedPattern + '$', 'i'); // Added 'i' flag for case-insensitivity
}
/**
* Creates and displays a custom reminder popup.
* @param {Object} reminder - The reminder object.
*/
function createCustomReminderPopup(reminder) {
// const popupId = 'custom-reminder-popup-' + reminder.id; // Old ID
if (document.getElementById('custom-reminder-display-popup')) { // Check for the new generic ID
// console.log(`[ContentScript Prisma] Custom reminder popup for ${reminder.name} already exists or another custom reminder is shown.`);
return; // Avoid showing multiple custom popups if one is already up with the generic ID
}
// addReminderStyles(); // Ensure styles are loaded // Removed call
const overlay = document.createElement('div');
overlay.className = 'reminder-overlay';
overlay.id = 'custom-reminder-overlay-' + reminder.id; // Keep overlay ID specific for now
// Ensure overlay doesn't stack if multiple custom popups appear (though shownCustomReminderIds should prevent this)
// And also check generic overlays from meta/ias
if (!document.querySelector('.reminder-overlay')) { // Simpler check: if ANY .reminder-overlay exists, don't add another one if this logic is flawed
document.body.appendChild(overlay);
} else if (!document.getElementById(overlay.id)) { // Only add if this specific overlay isn't already there
// If an overlay for another reminder (meta, ias, or another custom) is already there,
// this new custom popup will appear over it, which is fine.
// The z-index for popups is higher than overlays.
// We still append this specific overlay to ensure its removal logic is tied to this popup.
document.body.appendChild(overlay);
}
const popup = document.createElement('div');
popup.id = 'custom-reminder-display-popup'; // Standardized ID
// Inline styles are removed, will be handled by addReminderStyles
popup.innerHTML = `
<h3>${escapeHTML(reminder.name)}</h3>
${reminder.popupMessage}
<button id="custom-reminder-display-close">Got it!</button>
`;
document.body.appendChild(popup);
const closeButton = document.getElementById('custom-reminder-display-close'); // Use new ID
closeButton.addEventListener('click', () => {
if (popup.parentNode) popup.parentNode.removeChild(popup);
if (overlay.parentNode) overlay.parentNode.removeChild(overlay); // Remove its specific overlay
// shownCustomReminderIds.add(reminder.id) happens in checkCustomReminders, which is correct.
console.log(`[ContentScript Prisma] Custom reminder popup for ${reminder.name} closed by user.`);
});
console.log(`[ContentScript Prisma] Custom reminder popup created for: ${reminder.name}`);
}
/**
* Adds a "GMI Chat" button to the workflow widget to open a pre-filled Teams chat.
*/
function handleGmiChatButton() {
const workflowWidget = document.querySelector('.workflow-widget-wrapper');
if (!workflowWidget || workflowWidget.querySelector('.gmi-chat-button')) {
return; // Exit if the widget doesn't exist or the button is already there
}
const gmiChatButton = document.createElement('button');
gmiChatButton.textContent = 'GMI Chat';
gmiChatButton.className = 'filter-button prisma-paste-button gmi-chat-button';
gmiChatButton.addEventListener('click', () => {
const clientNameElement = document.querySelector('#gwt-debug-0-idesk-csl-product-label');
const campaignNameElement = document.querySelector('#gwt-debug-campaign-name');
const clientName = clientNameElement ? clientNameElement.textContent.trim() : 'CLIENT_NAME_HERE';
const campaignName = campaignNameElement ? campaignNameElement.getAttribute('title').trim() : 'CAMPAIGN_NAME_HERE';
const currentUrl = window.location.href;
const message = `${clientName} - ${campaignName}`;
const teamsUrl = `https://teams.microsoft.com/l/chat/0/0?users=edwin.balagopalan@wppmedia.com,ellie.vigors@wppmedia.com,harry.barnes@wppmedia.com,isobel.shaw@wppmedia.com,jett.hudson@wppmedia.com,lauren.pringle@wppmedia.com,matt.akerman@wppmedia.com,mihaela.lupu@wppmedia.com,rita.bressi@wppmedia.com,santiago.feberero@wppmedia.com,scott.moore@wppmedia.com,shreya.gurung@wppmedia.com,trish.costa@wppmedia.com&message=${encodeURIComponent(message)}%20${encodeURIComponent(currentUrl)}`;
window.open(teamsUrl, '_blank');
});
workflowWidget.appendChild(gmiChatButton);
}
/**
* Checks all active custom reminders against the current page URL and content.
* Displays a popup for the first matching reminder.
*/
function checkCustomReminders() {
console.log("[ContentScript Prisma] Running checkCustomReminders...");
if (activeCustomReminders.length === 0) {
console.log("[ContentScript Prisma] No active custom reminders to check.");
return;
}
// If a custom reminder is already displayed using the generic ID, don't try to show another one.
if (document.getElementById('custom-reminder-display-popup')) {
console.log("[ContentScript Prisma] Another custom reminder popup is already visible. Skipping further checks.");
// Log which reminder is being skipped IF we were iterating, but here we are aborting early.
// To log the specific reminder that would have been shown, this check needs to be inside the loop.
// However, the current logic is to prevent ANY new custom reminder if one is up.
return;
}
const currentUrl = window.location.href;
const pageText = document.body.innerText.toLowerCase(); // For case-insensitive search
for (const reminder of activeCustomReminders) {
console.log("[ContentScript Prisma] Checking custom reminder:", reminder.name, "ID:", reminder.id);
console.log("[ContentScript Prisma] Current URL:", currentUrl);
console.log("[ContentScript Prisma] Reminder URL Pattern:", reminder.urlPattern);
if (shownCustomReminderIds.has(reminder.id)) {
console.log("[ContentScript Prisma] Reminder", reminder.name, "already shown on this page load. Skipping.");
continue;
}
// Moved this check inside the loop so we can log which reminder is skipped due to an existing popup
if (document.getElementById('custom-reminder-display-popup')) {
console.log("[ContentScript Prisma] Another custom reminder popup is already visible. Skipping reminder:", reminder.name);
// Since only one custom popup can be shown at a time due to the generic ID,
// if one is already up, we must not attempt to show another.
// We can break here as no other reminder can be shown.
break;
}
const urlRegex = wildcardToRegex(reminder.urlPattern);
console.log("[ContentScript Prisma] Generated Regex:", urlRegex.toString());
const urlMatches = urlRegex.test(currentUrl);
console.log("[ContentScript Prisma] URL Match Result:", urlMatches);
if (urlMatches) {
console.log("[ContentScript Prisma] Reminder Text Trigger:", reminder.textTrigger);
let textMatch = false;
if (reminder.textTrigger && reminder.textTrigger.trim() !== '') {
const triggerTexts = reminder.textTrigger.split(',').map(t => t.trim().toLowerCase()).filter(t => t.length > 0);
if (triggerTexts.length > 0) {
if (triggerTexts.some(text => {
const pageIncludesText = pageText.includes(text);
// console.log(`[ContentScript Prisma] Checking page text for: "${text}", Found: ${pageIncludesText}`); // Potentially too verbose
return pageIncludesText;
})) {
textMatch = true;
}
} else {
textMatch = true; // Text trigger was defined but empty after trim/split (e.g. ", ,") means match.
}
} else {
textMatch = true; // No text trigger defined, URL match is enough
}
console.log("[ContentScript Prisma] Text Match Result:", textMatch);
if (textMatch) {
console.log("[ContentScript Prisma] Conditions MET for custom reminder:", reminder.name);
createCustomReminderPopup(reminder);
shownCustomReminderIds.add(reminder.id);
// Since custom popups now use a generic ID, we should break after finding the first one to show.
// This prevents multiple custom reminders from trying to use the same popup ID simultaneously.
break;
}
}
}
console.log("[ContentScript Prisma] Finished checkCustomReminders.");
}
/**
* Handles UI modifications and automation for the Campaign Management page.
* Hides specific sections and automates form fields based on settings.
*/
function handleCampaignManagementFeatures() {
if (!window.location.href.includes('osModalId=prsm-cm-cmpadd')) {
return;
}
if (!chrome.runtime || !chrome.runtime.id) return; // Context guard
chrome.storage.sync.get(['hidingSectionsEnabled', 'automateFormFieldsEnabled'], (data) => {
if (chrome.runtime.lastError) return; // Error guard
if (data.hidingSectionsEnabled !== false) {
// Hide sections
const objectiveSection = document.querySelector('fieldset.sectionObjective');
if (objectiveSection) {
objectiveSection.style.display = 'none';
}
const targetingSection = document.querySelector('fieldset.sectionTargeting');
if (targetingSection) {
targetingSection.style.display = 'none';
}
// Hide Flighting section
const flightingSelect = document.querySelector('#gwt-debug-distribution');
if (flightingSelect) {
const controlGroupDiv = flightingSelect.parentElement;
if (controlGroupDiv) {
const outerDiv = controlGroupDiv.parentElement;
if (outerDiv) {
outerDiv.style.display = 'none';
}
}
}
}
if (data.automateFormFieldsEnabled !== false) {
// Automate Media Mix field
const mediaTypeSelect = document.getElementById('debug-mediaMix-mediaType0'); // Corrected ID
if (mediaTypeSelect && !mediaMixAutomated) {
const onlineOption = mediaTypeSelect.querySelector('option[value="media_digital"]');
if (onlineOption) {
mediaTypeSelect.value = 'media_digital';
mediaTypeSelect.dispatchEvent(new Event('change', { bubbles: true }));
mediaTypeSelect.dispatchEvent(new Event('input', { bubbles: true })); // For Knockout.js
mediaMixAutomated = true;
}
}
// Automate Budget Type field
const budgetTypeSelect = document.getElementById('gwt-debug-budgetType'); // Corrected ID
if (budgetTypeSelect && !budgetTypeAutomated) {
const totalCostOption = budgetTypeSelect.querySelector('option[value="3"]');
if (totalCostOption) {
budgetTypeSelect.value = '3';
budgetTypeSelect.dispatchEvent(new Event('change', { bubbles: true }));
budgetTypeSelect.dispatchEvent(new Event('input', { bubbles: true })); // For Knockout.js
budgetTypeAutomated = true;
}
}
}
});
}
// --- Approver Pasting Feature ---
/**
* Adds buttons to paste approver emails and manage favorites on the relevant page.
*/
function handleApproverPasting() {
const selectors = {
toLabel: 'label',
selectContainer: '.select2-choices',
firstResult: '.select2-result-selectable'
};
// Find the "To" label on the page
const toLabel = Array.from(document.querySelectorAll(selectors.toLabel)).find(label => label.textContent.trim() === 'To');
if (!toLabel) {
return; // 'To:' label not found, so do nothing.
}
const buttonContainer = toLabel.parentNode;
// Check if the buttons are already added
if (buttonContainer.querySelector('.filter-button')) {
return;
}
// Create and add the "Paste approvers" button
const pasteButton = document.createElement('button');
pasteButton.textContent = 'Paste Approvers';
pasteButton.className = 'filter-button prisma-paste-button';
pasteButton.style.marginLeft = '10px';
const pasteFavouritesButton = document.createElement('button');
pasteFavouritesButton.textContent = 'Favourites';
pasteFavouritesButton.className = 'filter-button prisma-paste-button';
pasteFavouritesButton.style.marginLeft = '5px';
pasteButton.addEventListener('click', async () => {
console.log('[Paste Logic] Start');
pasteButton.disabled = true;
pasteButton.textContent = 'Pasting...';
let originalClipboard = '';
try {
// 1. Read the full list of emails from the clipboard.
const initialResponse = await chrome.runtime.sendMessage({ action: 'getClipboardText' });
console.log('[Paste Logic] Read initial clipboard:', initialResponse);
if (initialResponse.status !== 'success' || !initialResponse.text) {
console.error('Could not read clipboard.');
return;
}
originalClipboard = initialResponse.text;
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const emails = originalClipboard.split(/[\n,;]+/).map(e => e.trim()).filter(e => emailRegex.test(e));
console.log(`[Paste Logic] Found ${emails.length} valid emails.`);
if (emails.length > 0) {
await pasteEmails(emails);
}
} catch (error) {
console.error('[Paste Logic] Error during paste operation:', error);
} finally {
// 4. Restore original clipboard.
if (originalClipboard) {
console.log('[Paste Logic] Restoring original clipboard.');
await chrome.runtime.sendMessage({ action: 'copyToClipboard', text: originalClipboard });
}
pasteButton.disabled = false;
pasteButton.textContent = 'Paste Approvers';
console.log('[Paste Logic] End');
}
});
pasteFavouritesButton.addEventListener('click', async () => {
pasteFavouritesButton.disabled = true;
pasteFavouritesButton.textContent = 'Pasting...';
try {
const response = await chrome.runtime.sendMessage({ action: 'getFavouriteApprovers' });
if (response.status === 'success') {
await pasteEmails(response.emails);
}
} catch (error) {
console.error('Error pasting favourite approvers:', error);
} finally {
pasteFavouritesButton.disabled = false;
pasteFavouritesButton.textContent = 'Favourites';
}
});
async function pasteEmails(emails) {
for (const email of emails) {
console.log(`[Paste Logic] Processing: ${email}`);
await chrome.runtime.sendMessage({ action: 'copyToClipboard', text: email });
const selectContainer = document.querySelector(selectors.selectContainer);
if (selectContainer) {
selectContainer.click();
} else {
console.error('[Paste Logic] Cannot find .select2-choices container.');
break;
}
try {
await waitForElement('.select2-search-field input', 500);
} catch (error) {
console.warn('[Paste Logic] Did not find select2 search input after click.', error);
}
document.execCommand('paste');
try {
const firstResult = await waitForElement(selectors.firstResult);
firstResult.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
await waitForElementToDisappear(selectors.firstResult);
} catch (error) {
console.warn('[Paste Logic] No search result found to click or it did not disappear.', error);
}
}
}
// Insert the button after the "To:" label
toLabel.parentNode.insertBefore(pasteButton, toLabel.nextSibling);
toLabel.parentNode.insertBefore(pasteFavouritesButton, pasteButton.nextSibling);
}
/**
* Adds a "Manage Favourites" button next to the "Clear" button.
*/
function handleManageFavouritesButton() {
const clearButton = Array.from(document.querySelectorAll('button.btn-link.mo-btn-link')).find(btn => btn.textContent.trim() === 'Clear');
if (!clearButton) {
return;
}
const buttonContainer = clearButton.parentNode;
if (buttonContainer.querySelector('.manage-favourites-button')) {
return;
}
const manageFavouritesButton = document.createElement('button');
manageFavouritesButton.textContent = 'Manage Favourites';
manageFavouritesButton.className = 'btn-link mo-btn-link manage-favourites-button';
manageFavouritesButton.addEventListener('click', () => {
chrome.runtime.sendMessage({ action: 'openApproversPage' });
});
clearButton.parentNode.insertBefore(manageFavouritesButton, clearButton.nextSibling);
}
// --- End Custom Reminder Functions ---
/**
* Checks if the current page is one where the logo should be replaced.
* @returns {boolean} True if the logo should be replaced, false otherwise.
*/
function shouldReplaceLogoOnThisPage() {
if (typeof window === 'undefined' || !window.location || !window.location.href) {
return false; // Guard against test environments where window/location might not be fully available
}
const url = window.location.href;
// Updated condition:
return url.includes('aura.mediaocean.com') || url.includes('prisma.mediaocean.com');
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', mainContentScriptInit);
} else {
mainContentScriptInit();
}
/**
* Main initialization function called when the DOM is ready.
* Sets up observers and initial checks.
*/
function mainContentScriptInit() {
console.log("[ContentScript Prisma] DOMContentLoaded or already loaded. Initializing checks.");
if (shouldReplaceLogoOnThisPage()) {
fetchCustomReminders(); // Fetch initial set of custom reminders
checkAndReplaceLogo();
setTimeout(() => {
checkForMetaConditions();
checkForIASConditions();
checkCustomReminders(); // Initial check for custom reminders
handleCampaignManagementFeatures();
}, 2000);
}
const observer = new MutationObserver(function(mutations) {
if (shouldReplaceLogoOnThisPage()) {
checkAndReplaceLogo();
// No need to iterate mutations for these checks, just run them if any mutation occurred
setTimeout(() => { // Debounce/delay slightly
checkForMetaConditions();
checkForIASConditions();
checkCustomReminders(); // Check for custom reminders on DOM changes
handleCampaignManagementFeatures();
handleApproverPasting();
handleManageFavouritesButton();
handleGmiChatButton();
}, 300);
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
/**
* Listener for messages from other parts of the extension.
*/
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("[ContentScript Prisma] Message received in listener:", request);
if (request.action === "checkLogoReplaceEnabled") {
console.log("[ContentScript Prisma] 'checkLogoReplaceEnabled' action received.");
if (shouldReplaceLogoOnThisPage()) {
checkAndReplaceLogo();
}
sendResponse({status: "Logo check processed by content script"});
} else if (request.action === "showMetaReminder") {
console.log("[ContentScript Prisma] 'showMetaReminder' action received. Attempting to create popup.");
metaReminderDismissed = false;
window.forceShowMetaReminder = true;
checkForMetaConditions();
sendResponse({status: "Meta reminder shown by content script"});
console.log("[ContentScript Prisma] Response sent for 'showMetaReminder'.");
} else if (request.action === "customRemindersUpdated") {
console.log("[ContentScript Prisma] Received 'customRemindersUpdated' message. Re-fetching reminders.");
fetchCustomReminders();
shownCustomReminderIds.clear(); // Allow all reminders to be shown again as settings/list might have changed
checkCustomReminders(); // Optional: re-check immediately after update
sendResponse({status: "Custom reminders re-fetched and IDs reset by content script"});
} else {
console.log("[ContentScript Prisma] Unknown action received or no action taken:", request.action);
}
return true; // Keep the message channel open for asynchronous response if needed
});
console.log("[ContentScript Prisma] Event listeners, including onMessage, should be set up now.");
} // End of initializeContentScript
})(); // End of IIFE