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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
import type { Page, Frame } from 'playwright';

// ---------------------------------------------------------------------------
// CSS Selectors – grouped by workflow area
// ---------------------------------------------------------------------------

export const SELECTORS = {
  // ---- Login ----
  login: {
    emailInput: '#email',
    passwordInput: '#password',
    submitButton: 'input.submit[type="submit"]',
  },

  // ---- EULA ----
  eula: {
    agreementModalSaveBtn: '#eulaAgreementModalSaveBtn',
    iAgreeButton: 'button:has-text("I Agree")',
    acceptButton: 'button:has-text("Accept")',
    tdlIAgreeButton: 'tdl-button:has-text("I Agree")',
    tdlAcceptButton: 'tdl-button:has-text("Accept")',
    iAgreeContinueLink: 'a:has-text("I Agree -- Continue")',
  },

  // ---- Class ----
  class: {
    classNameLink: 'td.class_name a, a.ib-class-link, a',
  },

  // ---- Assignment ----
  assignment: {
    assignmentRow: 'tr.assignment-row',
    openButton: 'a.btn-open, a.btn-primary, button:has-text("Open"), a:has-text("Open")',
    allOpenButtons:
      'tr.assignment-row a.btn-open, a.btn-open, a.btn-primary, button:has-text("Open"), a:has-text("Open")',
  },

  // ---- Upload ----
  upload: {
    fileInput: 'input[type="file"]',
    browseButton:
      // New UI: tii-ing-upload-button > tdl-button[part="upload-form-container-button"]
      // Old UI: tdl-button with "Browse Files" text
      'tdl-button[part="upload-form-container-button"], tii-ing-upload-button tdl-button, button:has-text("Browse Files"), button:has-text("Browse"), tdl-button:has-text("Browse")',
    uploadFromDevice:
      'tdl-button[part="upload-form-container-menu-browse-button"], .actions-menu tdl-button[part="upload-form-container-menu-browse-button"], button:has-text("Your device"), tdl-button:has-text("Your device"), button:has-text("Upload from this device"), tdl-button:has-text("Upload from this device")',
    submitButton:
      // New UI: tdl-button with slot="form-submit-button-label" containing "Upload and Preview"
      // Also handles plain Submit/Confirm after preview, plus accept-button/tii-grn-button
      'tdl-button[part="submit-button"], tdl-button:has-text("Upload and Preview"), tdl-button.accept-button, tii-grn-button, button:has-text("Upload"), tdl-button:has-text("Upload"), button:has-text("Confirm"), tdl-button:has-text("Confirm"), button:has-text("Submit"), tdl-button:has-text("Submit")',
    // New UI container for upload step
    uploadStepContainer: 'div.upload-step, tii-ing-dropzone',
  },

  // ---- Resubmit ----
  resubmit: {
    // New UI: tii-grn-button with text "Resubmit" (inside tii-workflow-student-summary-panel-new)
    // Old UI: tdl-button or button with text "Resubmit"
    resubmitButton:
      'tii-grn-button:has-text("Resubmit"), button:has-text("Resubmit"), tdl-button:has-text("Resubmit"), [aria-label*="Resubmit"]',
    // New submission action wrapper (contains Submit/Resubmit button)
    submissionActionPanel: 'tii-workflow-student-summary-panel-new, div.submission-action',
    // New resubmission confirmation modal (dialog element)
    resubmissionConfirmModal: 'dialog#tii-resubmission-last-submission-modal',
    // Accept button inside the new modal (slot-based)
    resubmissionConfirmAccept:
      'dialog#tii-resubmission-last-submission-modal [slot="accept-button"] tdl-button, dialog#tii-resubmission-last-submission-modal .accept-button, dialog#tii-resubmission-last-submission-modal tdl-button.accept-button',
  },

  // ---- Submission Card / Similarity ----
  similarity: {
    viewSubmissionButton:
      'button.link-button[aria-label*="View submission"], button.link-button, a[aria-label*="View submission"], a.view-mark, .similarity-button',
    similarityDisplay: 'tii-mpdd-similarity-display',
    similarityBadge: 'span[part="tii-grn-badge-label"]',
    similaritySpan: 'tii-mpdd-similarity-display span:has-text("Similarity:")',
    bodyContainer: '.body-container',
    linkButtons: 'button.link-button',
  },

  // ---- Viewer ----
  viewer: {
    titleButton:
      'button.link-button[aria-label*="View submission"], button.link-button, a[aria-label*="View submission"], a.view-mark, .similarity-button, [part="tii-grn-badge-label"]',
  },

  // ---- Filters ----
  filters: {
    similarityTab:
      [
        'tii-sws-tab-button#tab-similarity',
        '#tab-similarity',
        'tii-sws-tab-button[aria-controls="tii-sws-main"]:has-text("Similarity")',
        'tii-sws-tab-button:has-text("Similarity")',
        'button.tab-button[data-px="SimilarityTabClicked"]',
        '[data-px="SimilarityTabClicked"]',
        '[with-data-px="SimilarityTabClicked"]',
        '[withdatapx="SimilarityTabClicked"]',
        '[data-px*="SimilarityTab"]',
        '[with-data-px*="SimilarityTab"]',
        '[withdatapx*="SimilarityTab"]',
        '[role="tab"]:has-text("Similarity")',
        'button:has-text("Similarity")',
        'tdl-button:has-text("Similarity")',
        'tdl-labeled-button:has-text("Similarity")',
        '[aria-label*="Similarity"]',
      ].join(', '),
    filterButton:
      [
        '.tii-SimilarityReportPanel[aria-hidden="false"] .tii-SimilarityReportPanelHeader__SettingsButton',
        '.tii-SimilarityReportPanel[aria-hidden="false"] tdl-button[with-data-px="SettingsClicked"]',
        '.tii-filters-and-exclusions-container tdl-button[with-data-px="SettingsClicked"]',
        'tdl-button[with-data-px="SettingsClicked"]',
        'tdl-button[withdatapx="SettingsClicked"]',
        '[with-data-px="SettingsClicked"]',
        '[withdatapx="SettingsClicked"]',
        '[data-px="SettingsClicked"]',
        '[data-px*="Settings"]',
        '[with-data-px*="Settings"]',
        '[withdatapx*="Settings"]',
        '.tii-SimilarityReportPanelHeader__SettingsButton',
        'tdl-button:has-text("Filters")',
        'tdl-labeled-button:has-text("Filters")',
        'tii-grn-button:has-text("Filters")',
        'button:has-text("Filters")',
        '[role="button"]:has-text("Filters")',
        '[aria-label*="Filter"]',
        '[title*="Filter"]',
        '[part*="filter"]',
      ].join(', '),
    excludeBibliography: '#excludeBibliography, [with-id="excludeBibliography"]',
    excludeQuotes: '#excludeQuotes, [with-id="excludeQuotes"]',
    excludeCitations: '#excludeCitations, [with-id="excludeCitations"]',
    excludeSmallMatches:
      'tdl-checkbox[with-px-label="ExcludeSmallMatches"], tdl-checkbox:has-text("Exclude small matches")',
    smallMatchesInput:
      'tdl-number-input.small-matches-input, tdl-number-input[label="Set match exclusion threshold"]',
    applyFilters:
      [
        'tdl-button[with-data-px="ApplyFiltersClicked"]',
        'tdl-button[withdatapx="ApplyFiltersClicked"]',
        '[with-data-px="ApplyFiltersClicked"]',
        '[withdatapx="ApplyFiltersClicked"]',
        '[data-px="ApplyFiltersClicked"]',
        'tdl-button[slot="accept-button"]:has-text("Apply Filters")',
        'tdl-button:has-text("Apply Filters")',
        'button:has-text("Apply Filters")',
      ].join(', '),
    backToReport:
      [
        '[id="supplement-container.viewSettings"] button.tii-similarity-supplement--back-button',
        '.tii-panel-overlay--is-open button.tii-similarity-supplement--back-button',
        'button.tii-similarity-supplement--back-button',
        '[data-px="FiltersBackToReportClicked"]',
        '[with-data-px="FiltersBackToReportClicked"]',
        '[withdatapx="FiltersBackToReportClicked"]',
        'button:has-text("Back to Similarity Report")',
        'tdl-button:has-text("Back to Similarity Report")',
      ].join(', '),
  },

  // ---- Download ----
  download: {
    downloadMenu:
      [
        'tii-sws-download-btn-mfe tdl-labeled-button',
        'tii-sws-download-btn-mfe',
        'tii-sws-header [slot="download-btn"]',
        'tdl-labeled-button[withdatapx="DownloadMenuClicked"]',
        '[withdatapx="DownloadMenuClicked"]',
        'tdl-labeled-button:has-text("Download")',
        'button:has-text("Download")',
      ].join(', '),
    currentViewOption:
      [
        '[with-data-px="DownloadOptionCurrentViewClicked"]',
        '[withdatapx="DownloadOptionCurrentViewClicked"]',
        '[data-px="DownloadOptionCurrentViewClicked"]',
      ].join(', '),
  },

  // ---- Quota ----
  quota: {
    warningConfirmButton:
      'tii-grn-button:has-text("Continue"), tii-grn-button.accept-button, tdl-button[slot="accept-button"], tdl-button[variety="filled"], tii-grn-button',
  },
};

// ---------------------------------------------------------------------------
// EULA selector list (iterated during acceptance)
// ---------------------------------------------------------------------------

export const EULA_SELECTORS = [
  SELECTORS.eula.agreementModalSaveBtn,
  SELECTORS.eula.iAgreeButton,
  SELECTORS.eula.acceptButton,
  SELECTORS.eula.tdlIAgreeButton,
  SELECTORS.eula.tdlAcceptButton,
  SELECTORS.eula.iAgreeContinueLink,
];

export const EULA_BODY_REGEX =
  /End User License Agreement|Terms of Use|I Agree|I Accept/i;

export const EULA_DEEP_CLICK_TEXTS = [
  'i agree',
  'i accept',
  'accept',
  'continue',
  'save',
];

export const EULA_BLOCK_REGEX =
  /Could not access the assignment|End User License Agreement needs to be accepted|EULA needs to be accepted/i;

// ---------------------------------------------------------------------------
// Quota warning confirmation labels (in priority order)
// ---------------------------------------------------------------------------

export const QUOTA_CONFIRM_LABELS = [
  'Continue to File Upload',
  'Continue',
  'Confirm',
  'I understand',
  'OK',
  'Yes',
];

// ---------------------------------------------------------------------------
// Shadow DOM helpers
// ---------------------------------------------------------------------------

type Scope = Page | Frame;

/**
 * Read all text from a scope, traversing into shadow DOMs.
 */
export async function readScopeDeepText(scope: Scope): Promise<string> {
  return scope.evaluate(() => {
    const roots: (Document | ShadowRoot)[] = [document];
    const seen = new Set<Element>();

    for (let i = 0; i < roots.length; i++) {
      const root = roots[i];
      for (const el of Array.from(root.querySelectorAll('*'))) {
        if (seen.has(el)) continue;
        seen.add(el);
        if (el.shadowRoot) roots.push(el.shadowRoot);
      }
    }

    return roots
      .map((r) => (r as any).body?.innerText || (r as any).host?.innerText || r.textContent || '')
      .join(' ')
      .replace(/\s+/g, ' ')
      .trim();
  }).catch(() => '');
}

/**
 * Read only visible text from a scope, traversing into shadow DOMs.
 */
export async function readScopeVisibleDeepText(scope: Scope): Promise<string> {
  return scope.evaluate(() => {
    const roots: (Document | ShadowRoot)[] = [document];
    const seen = new Set<Element>();

    for (let i = 0; i < roots.length; i++) {
      const root = roots[i];
      for (const el of Array.from(root.querySelectorAll('*'))) {
        if (seen.has(el)) continue;
        seen.add(el);
        if (el.shadowRoot) roots.push(el.shadowRoot);
      }
    }

    const isVisible = (el: Element): boolean => {
      if (!(el instanceof HTMLElement)) return false;
      const style = window.getComputedStyle(el);
      const rect = el.getBoundingClientRect();
      return (
        style.visibility !== 'hidden' &&
        style.display !== 'none' &&
        rect.width > 0 &&
        rect.height > 0
      );
    };

    const chunks: string[] = [];
    for (const root of roots) {
      for (const el of Array.from(root.querySelectorAll('*'))) {
        if (!isVisible(el)) continue;
        const text = (el as HTMLElement).innerText || '';
        if (text) chunks.push(text);
      }
    }

    return chunks.join(' ').replace(/\s+/g, ' ').trim();
  }).catch(() => '');
}

/**
 * Check if any of textMatches appear in deep text of the scope.
 */
export async function scopeHasDeepText(
  scope: Scope,
  textMatches: string[],
): Promise<boolean> {
  const text = await readScopeDeepText(scope);
  const normalized = text.toLowerCase();
  return textMatches.some((m) => normalized.includes(m.toLowerCase()));
}

/**
 * Check if any of textMatches appear in visible deep text of the scope.
 * This avoids matching hidden web-component templates that Turnitin keeps in
 * the DOM for states that are not currently active.
 */
export async function scopeHasVisibleDeepText(
  scope: Scope,
  textMatches: string[],
): Promise<boolean> {
  const text = await readScopeVisibleDeepText(scope);
  const normalized = text.toLowerCase();
  return textMatches.some((m) => normalized.includes(m.toLowerCase()));
}

/**
 * Click a visible checkbox in the given scope.
 */
export async function clickVisibleCheckbox(scope: Scope): Promise<boolean> {
  return scope
    .evaluate(() => {
      const isVisible = (el: Element): boolean => {
        const style = window.getComputedStyle(el);
        const rect = el.getBoundingClientRect();
        return (
          style.visibility !== 'hidden' &&
          style.display !== 'none' &&
          rect.width > 0 &&
          rect.height > 0
        );
      };

      const cb = Array.from(
        document.querySelectorAll(
          'input[type="checkbox"], tdl-checkbox, tii-grn-checkbox',
        ),
      ).find(isVisible);
      if (!cb) return false;
      (cb as HTMLElement).click();
      return true;
    })
    .catch(() => false);
}

/**
 * Traverse shadow DOM looking for clickable elements whose text matches one of
 * the given strings, then click the first match.
 */
export async function deepClickByText(
  scope: Scope,
  textMatches: string[],
): Promise<boolean> {
  return scope
    .evaluate((matches: string[]) => {
      const normalizedMatches = matches.map((v) => v.toLowerCase());
      const roots: (Document | ShadowRoot)[] = [document];
      const seen = new Set<Element>();

      for (let i = 0; i < roots.length; i++) {
        const root = roots[i];
        for (const el of Array.from(root.querySelectorAll('*'))) {
          if (seen.has(el)) continue;
          seen.add(el);
          if (el.shadowRoot) roots.push(el.shadowRoot);
        }
      }

      const isVisible = (el: Element): boolean => {
        if (!(el instanceof HTMLElement)) return false;
        const style = window.getComputedStyle(el);
        const rect = el.getBoundingClientRect();
        return (
          style.visibility !== 'hidden' &&
          style.display !== 'none' &&
          rect.width > 0 &&
          rect.height > 0
        );
      };

      const candidates: Element[] = [];
      for (const root of roots) {
        candidates.push(
          ...Array.from(
            root.querySelectorAll(
              'button, a, input, tdl-button, tii-grn-button, tdl-labeled-button',
            ),
          ),
        );
      }

      const target = candidates.find((el) => {
        if (!isVisible(el)) return false;
        const text = [
          (el as HTMLElement).innerText,
          el.textContent,
          el.getAttribute('aria-label'),
          el.getAttribute('value'),
        ]
          .filter(Boolean)
          .join(' ')
          .replace(/\s+/g, ' ')
          .trim()
          .toLowerCase();
        return normalizedMatches.some((m) => text.includes(m));
      });

      if (!target) return false;
      (target as HTMLElement).click();
      return true;
    }, textMatches)
    .catch(() => false);
}

/**
 * Find the scope (page or frame) that contains a visible element matching the
 * given selector.
 */
export async function findAssignmentScope(
  page: Page,
  selector: string,
  timeoutMs = 1000,
): Promise<Page | Frame | null> {
  if (
    await page
      .locator(selector)
      .first()
      .isVisible({ timeout: timeoutMs })
      .catch(() => false)
  )
    return page;

  for (const frame of page.frames()) {
    if (frame.url().includes('cookie-shim')) continue;
    if (
      await frame
        .locator(selector)
        .first()
        .isVisible({ timeout: timeoutMs })
        .catch(() => false)
    )
      return frame;
  }

  return null;
}

/**
 * Find the scope (page or frame) that contains deep text matching any of the
 * given strings, polling until timeoutMs.
 */
export async function findScopeByDeepText(
  page: Page,
  textMatches: string[],
  timeoutMs = 30000,
): Promise<Page | Frame | null> {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    if (await scopeHasDeepText(page, textMatches)) return page;

    for (const frame of page.frames()) {
      if (frame.url().includes('cookie-shim')) continue;
      if (await scopeHasDeepText(frame, textMatches)) return frame;
    }

    await page.waitForTimeout(1000);
  }

  return null;
}

/**
 * Resolve the best scope for the assignment page.
 */
export async function resolveAssignmentScope(
  page: Page,
): Promise<Page | Frame> {
  return (
    (await findAssignmentScope(
      page,
      'button.link-button[aria-label*="View submission"], tii-mpdd-similarity-display',
      1000,
    )) ||
    (await findAssignmentScope(
      page,
      'tdl-button[part="upload-form-container-button"], button:has-text("Browse Files"), input[type="file"]',
      1000,
    )) ||
    page
  );
}