Spaces:
Sleeping
Sleeping
File size: 8,809 Bytes
9e212e3 | 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 | import type { Page, Frame } from 'playwright';
import {
findAssignmentScope,
readScopeVisibleDeepText,
scopeHasVisibleDeepText,
} from '../selectors';
import { SELECTORS } from '../selectors';
type Scope = Page | Frame;
export interface SubmissionState {
scope: Scope | null;
hasExistingSubmission: boolean;
hasResubmitAction: boolean;
hasUploadForm: boolean;
}
async function hasVisible(scope: Scope, selector: string, timeoutMs = 1000): Promise<boolean> {
return scope.locator(selector).first().isVisible({ timeout: timeoutMs }).catch(() => false);
}
async function hasVisibleActionText(scope: Scope, textMatches: string[]): Promise<boolean> {
return scope
.evaluate((matches: string[]) => {
const normalizedMatches = matches.map((value) => value.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, [role="button"]',
),
),
);
}
return candidates.some((el) => {
if (!isVisible(el)) return false;
const text = [
(el as HTMLElement).innerText,
el.textContent,
el.getAttribute('aria-label'),
el.getAttribute('value'),
el.getAttribute('part'),
el.getAttribute('data-px'),
el.getAttribute('with-data-px'),
el.getAttribute('with-px-label'),
]
.filter(Boolean)
.join(' ')
.replace(/\s+/g, ' ')
.trim()
.toLowerCase();
if (
text.includes('setting info') ||
text.includes('resubmissions are allowed') ||
text.includes('late submissions are allowed') ||
text.includes('collapse details') ||
text.includes('assignment details') ||
text.includes('rubric') ||
text.includes('template')
) {
return false;
}
return normalizedMatches.some((match) => text.includes(match));
});
}, textMatches)
.catch(() => false);
}
async function hasInitialSubmitAction(scope: Scope): Promise<boolean> {
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 hasAncestorSignal = (el: Element): boolean => {
let current: Element | null = el;
const visited = new Set<Element>();
while (current && !visited.has(current)) {
visited.add(current);
const className = current.getAttribute('class') || '';
const slot = current.getAttribute('slot') || '';
const tag = current.tagName.toLowerCase();
if (
slot === 'submission-action' ||
className.includes('submission-action') ||
className.includes('submission-slot-container') ||
tag === 'tii-workflow-student-summary-panel-new' ||
tag === 'tii-workflow-lfw-student-show-assignment'
) {
return true;
}
const parent: HTMLElement | null = current.parentElement;
if (parent) {
current = parent;
continue;
}
const root = current.getRootNode();
current = root instanceof ShadowRoot ? root.host : null;
}
return false;
};
for (const root of roots) {
for (const el of Array.from(
root.querySelectorAll('tii-grn-button, button, tdl-button, [role="button"], input[type="submit"]'),
)) {
if (!isVisible(el)) continue;
const text = [
(el as HTMLElement).innerText,
el.textContent,
el.getAttribute('aria-label'),
el.getAttribute('value'),
]
.filter(Boolean)
.join(' ')
.replace(/\s+/g, ' ')
.trim();
const normalized = text.toLowerCase();
if (!/(^|\s)submit(\s|$)/i.test(text)) continue;
if (
normalized.includes('resubmit') ||
normalized.includes('submit file') ||
normalized.includes('upload and preview') ||
normalized.includes('collapse details') ||
normalized.includes('setting info')
) {
continue;
}
if (hasAncestorSignal(el)) return true;
}
}
return false;
})
.catch(() => false);
}
/**
* Detect what Turnitin is currently showing after opening an assignment.
* This is intentionally based on the live page, because account history can
* differ from the requested job mode.
*/
export async function detectSubmissionState(page: Page): Promise<SubmissionState> {
const scope =
(await findAssignmentScope(
page,
[
SELECTORS.resubmit.resubmitButton,
SELECTORS.similarity.viewSubmissionButton,
SELECTORS.upload.browseButton,
SELECTORS.upload.fileInput,
SELECTORS.resubmit.submissionActionPanel,
'tii-workflow-lfw-student-show-assignment',
'div[slot="submission-action"].submission-action',
'tii-grn-button:has-text("Submit")',
].join(', '),
3000,
));
if (!scope) {
return {
scope: null,
hasExistingSubmission: false,
hasResubmitAction: false,
hasUploadForm: false,
};
}
const visibleText = (await readScopeVisibleDeepText(scope)).toLowerCase();
const hasSubmitAction = await hasInitialSubmitAction(scope);
const hasResubmitAction =
(await hasVisible(scope, SELECTORS.resubmit.resubmitButton, 1500)) ||
(await hasVisible(
scope,
'a:has-text("Resubmit"), button:has-text("Resubmit"), tdl-button:has-text("Resubmit"), [aria-label*="Resubmit"], [data-px*="Resubmit"]',
1500,
)) ||
(await hasVisibleActionText(scope, ['resubmit']));
const hasUploadForm =
(await hasVisible(scope, SELECTORS.upload.fileInput, 1500)) ||
(await hasVisible(scope, SELECTORS.upload.browseButton, 1500)) ||
hasSubmitAction ||
visibleText.includes('browse files') ||
visibleText.includes('drag and drop file') ||
visibleText.includes('your device');
const hasViewSubmissionAction =
(await hasVisible(
scope,
'button.link-button[aria-label*="View submission"], a[aria-label*="View submission"], a.view-mark, .similarity-button',
1500,
)) ||
(await hasVisibleActionText(scope, ['view submission']));
const hasExistingSubmission =
hasResubmitAction ||
hasViewSubmissionAction ||
(await hasVisible(scope, SELECTORS.similarity.similarityDisplay, 1500)) ||
(await hasVisible(scope, SELECTORS.similarity.similarityBadge, 1500)) ||
(visibleText.includes('similarity:') || visibleText.includes('view submission')) ||
((await scopeHasVisibleDeepText(scope, ['submitted'])) && !hasUploadForm);
return {
scope,
// If the first-submission upload form is visible and there is no visible
// submission action, prefer upload. Turnitin web components often keep
// hidden "Resubmit" text in the DOM before any file exists.
hasExistingSubmission: hasUploadForm && !hasResubmitAction && !hasViewSubmissionAction
? false
: hasExistingSubmission,
hasResubmitAction,
hasUploadForm,
};
}
|