Skip to content

Commit fcdefe5

Browse files
committed
checkpoint automation changes
1 parent da20453 commit fcdefe5

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

injected/src/features/autofill-password-import.js

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export default class AutofillPasswordImport extends ContentFeature {
5252

5353
#domLoaded;
5454

55+
#currentLocation;
56+
57+
#isBookmarkModalVisible = false;
58+
#isBookmarkProcessed = false;
59+
5560
/** @type {WeakSet<Element>} */
5661
#tappedElements = new WeakSet();
5762

@@ -422,8 +427,12 @@ export default class AutofillPasswordImport extends ContentFeature {
422427
}
423428
}
424429

425-
handleBookmarkImportPath(pathname) {
430+
async handleBookmarkImportPath(pathname) {
426431
console.log('DEEP DEBUG autofill-password-import: handleBookmarkImportPath', pathname);
432+
if (pathname === '/' && !this.#isBookmarkModalVisible) {
433+
await this.clickDisselectAllButton();
434+
await this.selectBookmark();
435+
}
427436
}
428437

429438
/**
@@ -508,7 +517,95 @@ export default class AutofillPasswordImport extends ContentFeature {
508517
this.#settingsButtonSettings = this.getFeatureSetting('settingsButton');
509518
}
510519

520+
/** Bookmark import code */
521+
get disselectAllButtonSelector() {
522+
return 'c-wiz[data-node-index="4;0"] button';
523+
}
524+
525+
get bookmarkSelectButtonSelector() {
526+
return 'fieldset.rcetic input';
527+
}
528+
529+
get chromeSectionSelector() {
530+
return 'c-wiz [data-id="chrome"]';
531+
}
532+
533+
get nextStepButtonSelector() {
534+
return 'div[data-jobid] > div:nth-of-type(2) button';
535+
}
536+
537+
get createExportButtonSelector() {
538+
return 'div[data-configure-step] button';
539+
}
540+
541+
async findDisselectAllButton() {
542+
return await withExponentialBackoff(() => document.querySelectorAll(this.disselectAllButtonSelector)[1]);
543+
}
544+
async selectBookmark() {
545+
if (this.#isBookmarkProcessed) {
546+
return;
547+
}
548+
const chromeDataButtonSelector = `${this.chromeSectionSelector} button`;
549+
const chromeDataButton = /** @type HTMLButtonElement */ (
550+
await withExponentialBackoff(() => document.querySelectorAll(chromeDataButtonSelector)[1], 5)
551+
);
552+
chromeDataButton?.focus();
553+
chromeDataButton?.click();
554+
this.#isBookmarkModalVisible = true;
555+
await this.domLoaded;
556+
const disselectAllButton = /** @type HTMLButtonElement */ (
557+
await withExponentialBackoff(() => document.querySelectorAll('fieldset.rcetic button')[1])
558+
);
559+
560+
disselectAllButton?.click();
561+
562+
const bookmarkSelectButton = /** @type HTMLInputElement */ (
563+
await withExponentialBackoff(() => document.querySelectorAll(this.bookmarkSelectButtonSelector)[1])
564+
);
565+
566+
await withExponentialBackoff(() => !bookmarkSelectButton?.checked);
567+
568+
bookmarkSelectButton?.click();
569+
570+
const okButton = /** @type HTMLButtonElement */ (document.querySelectorAll('div[role="button"]')[7]);
571+
572+
await withExponentialBackoff(() => okButton.ariaDisabled !== 'true');
573+
574+
okButton?.click();
575+
this.#isBookmarkModalVisible = false;
576+
this.#isBookmarkProcessed = true;
577+
578+
const nextStepButton = /** @type HTMLButtonElement */ (document.querySelectorAll(this.nextStepButtonSelector)[0]);
579+
nextStepButton?.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
580+
nextStepButton?.click();
581+
582+
const createExportButton = /** @type HTMLButtonElement */ (document.querySelectorAll(this.createExportButtonSelector)[0]);
583+
createExportButton?.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
584+
createExportButton?.click();
585+
}
586+
587+
async clickDisselectAllButton() {
588+
const element = /** @type HTMLButtonElement */ (await this.findDisselectAllButton());
589+
console.log('Deep element', element);
590+
if (element != null) {
591+
element.click();
592+
}
593+
594+
const chromeSectionElement = /** @type HTMLInputElement */ (
595+
await withExponentialBackoff(() => document.querySelectorAll(this.chromeSectionSelector)[0].querySelector('input'))
596+
);
597+
console.log('DEEP chromeSectionElement', chromeSectionElement);
598+
599+
// First wait for the element to become unchecked (due to slow disselection)
600+
await withExponentialBackoff(() => !chromeSectionElement?.checked);
601+
602+
chromeSectionElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
603+
604+
chromeSectionElement?.click();
605+
}
606+
511607
urlChanged() {
608+
console.log('DEEP DEBUG autofill-password-import: urlChanged', window.location);
512609
this.handleLocation(window.location);
513610
}
514611

injected/src/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,22 +803,22 @@ export function legacySendMessage(messageType, options) {
803803
}
804804

805805
/**
806-
* Takes a function that returns an element and tries to find it with exponential backoff.
806+
* Takes a function that returns an element and tries to execute it until it returns a valid result or the max attempts are reached.
807807
* @param {number} delay
808808
* @param {number} [maxAttempts=4] - The maximum number of attempts to find the element.
809809
* @param {number} [delay=500] - The initial delay to be used to create the exponential backoff.
810-
* @returns {Promise<Element|HTMLElement|null>}
810+
* @returns {Promise<any>}
811811
*/
812812
export function withExponentialBackoff(fn, maxAttempts = 4, delay = 500) {
813813
return new Promise((resolve, reject) => {
814814
let attempts = 0;
815815
const tryFn = () => {
816816
attempts += 1;
817-
const error = new Error('Element not found');
817+
const error = new Error('Result is invalid');
818818
try {
819-
const element = fn();
820-
if (element) {
821-
resolve(element);
819+
const result = fn();
820+
if (result) {
821+
resolve(result);
822822
} else if (attempts < maxAttempts) {
823823
setTimeout(tryFn, delay * Math.pow(2, attempts));
824824
} else {

0 commit comments

Comments
 (0)