Fix patrol delete condition handling
This commit is contained in:
100
app/amazon/scripts/patrol_delete/complete_draft_select_all.js
Normal file
100
app/amazon/scripts/patrol_delete/complete_draft_select_all.js
Normal file
@@ -0,0 +1,100 @@
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function clickElement(el) {
|
||||
el.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
if (el.focus) el.focus();
|
||||
for (const eventName of ['pointerdown', 'mousedown', 'pointerup', 'mouseup']) {
|
||||
el.dispatchEvent(new MouseEvent(eventName, { bubbles: true, cancelable: true, view: window }));
|
||||
}
|
||||
el.click();
|
||||
}
|
||||
|
||||
function summarizeRow(row) {
|
||||
return {
|
||||
rowId: row.getAttribute('row-id') || '',
|
||||
text: textOf(row).slice(0, 500),
|
||||
selected: row.getAttribute('aria-selected') === 'true' || row.classList.contains('ag-row-selected'),
|
||||
};
|
||||
}
|
||||
|
||||
const payload = arguments[0] || {};
|
||||
const view = document.querySelector('[data-testid="complete-drafts-view"]');
|
||||
if (!view) return { ok: false, reason: 'complete drafts view not found' };
|
||||
|
||||
const rows = Array.from(view.querySelectorAll('.ag-center-cols-container [role="row"]'))
|
||||
.filter((row) => isVisible(row));
|
||||
const rowCount = rows.length;
|
||||
const selectedBefore = rows.filter((row) => row.getAttribute('aria-selected') === 'true' || row.classList.contains('ag-row-selected')).length;
|
||||
const maxSelectCountRaw = Number(payload.maxSelectCount || 0);
|
||||
const maxSelectCount = Number.isFinite(maxSelectCountRaw) && maxSelectCountRaw > 0 ? Math.floor(maxSelectCountRaw) : 0;
|
||||
|
||||
if (payload.probeOnly) {
|
||||
return {
|
||||
ok: true,
|
||||
probeOnly: true,
|
||||
rowCount,
|
||||
selectedCount: selectedBefore,
|
||||
rows: rows.slice(0, 10).map(summarizeRow),
|
||||
};
|
||||
}
|
||||
|
||||
if (rowCount <= 0) {
|
||||
return { ok: false, reason: 'no complete draft rows', rowCount, selectedCount: selectedBefore };
|
||||
}
|
||||
|
||||
const headerCheckbox = view.querySelector('.ag-header-select-all input[type="checkbox"]');
|
||||
if (!headerCheckbox) {
|
||||
return { ok: false, reason: 'complete draft select-all checkbox not found', rowCount, selectedCount: selectedBefore };
|
||||
}
|
||||
|
||||
if (maxSelectCount > 0 && maxSelectCount < rowCount) {
|
||||
if (headerCheckbox.checked) {
|
||||
clickElement(headerCheckbox);
|
||||
}
|
||||
for (const row of rows) {
|
||||
if (row.getAttribute('aria-selected') === 'true' || row.classList.contains('ag-row-selected')) {
|
||||
const checkbox = row.querySelector('input[type="checkbox"]');
|
||||
if (checkbox && checkbox.checked) clickElement(checkbox);
|
||||
}
|
||||
}
|
||||
for (const row of rows.slice(0, maxSelectCount)) {
|
||||
const checkbox = row.querySelector('input[type="checkbox"]');
|
||||
if (checkbox && !checkbox.checked) {
|
||||
clickElement(checkbox);
|
||||
} else if (!checkbox) {
|
||||
clickElement(row);
|
||||
}
|
||||
}
|
||||
} else if (!headerCheckbox.checked || selectedBefore < rowCount) {
|
||||
clickElement(headerCheckbox);
|
||||
}
|
||||
|
||||
let selectedRows = Array.from(view.querySelectorAll('.ag-center-cols-container [role="row"]'))
|
||||
.filter((row) => isVisible(row) && (row.getAttribute('aria-selected') === 'true' || row.classList.contains('ag-row-selected')));
|
||||
|
||||
if (selectedRows.length <= 0) {
|
||||
const firstRowCheckbox = rows[0] && rows[0].querySelector('input[type="checkbox"]');
|
||||
if (firstRowCheckbox && !firstRowCheckbox.checked) {
|
||||
clickElement(firstRowCheckbox);
|
||||
}
|
||||
}
|
||||
|
||||
selectedRows = Array.from(view.querySelectorAll('.ag-center-cols-container [role="row"]'))
|
||||
.filter((row) => isVisible(row) && (row.getAttribute('aria-selected') === 'true' || row.classList.contains('ag-row-selected')));
|
||||
|
||||
return {
|
||||
ok: selectedRows.length > 0,
|
||||
rowCount,
|
||||
selectedCount: selectedRows.length,
|
||||
headerChecked: headerCheckbox.checked,
|
||||
rows: selectedRows.slice(0, 10).map(summarizeRow),
|
||||
};
|
||||
Reference in New Issue
Block a user