Skip to content

Commit

Permalink
fix: re-enable ESM only scanner debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Jul 21, 2024
1 parent 88c8291 commit 951e55e
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 57 deletions.
16 changes: 7 additions & 9 deletions dist/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions dist/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/DeviceInterface/InterfacePrototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {DeviceApi} from '../../packages/device-api/index.js'
import {
GetAutofillCredentialsCall,
StoreFormDataCall,
SendJSPixelCall
SendJSPixelCall,
AddDebugFlagCall
} from '../deviceApiCalls/__generated__/deviceApiCalls.js'
import {initFormSubmissionsApi} from './initFormSubmissionsApi.js'
import {EmailProtection} from '../EmailProtection.js'
Expand Down Expand Up @@ -313,6 +314,9 @@ class InterfacePrototype {

postInit () {
const cleanup = this.scanner.init()
if (this.globalConfig.isExtension) {
this.deviceApi.notify(new AddDebugFlagCall({ flag: 'autofill' }))
}
this.addLogoutListener(() => {
cleanup('Logged out')
if (this.globalConfig.isDDGDomain) {
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Form {
this.device = deviceInterface
this.hasShadowTree = hasShadowTree

/** @type Record<'all' | SupportedMainTypes, Set> */
/** @type {Record<'all' | SupportedMainTypes, Set>} */
this.inputs = {
all: new Set(),
credentials: new Set(),
Expand Down
86 changes: 79 additions & 7 deletions src/Scanner.debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createScanner } from './Scanner.js'

sessionStorage.setItem('ddg-autofill-debug', 'true')

/**
* Scanner.debug.js
*
Expand All @@ -21,6 +19,12 @@ const mockInterface = {
inputType_credentials: true,
inputType_identities: true,
inputType_creditCards: true
},
populateDataIfNeeded: () => {
// console.log('populateDataIfNeeded');
},
canAutofillType: () => {
return true
}
},
globalConfig: {
Expand All @@ -34,12 +38,80 @@ const mockInterface = {
},
attachTooltip (...args) {
console.log('device.attachTooltip', args)
},
isTooltipActive: () => {
return false
},
get scanner() {

Check failure on line 45 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Missing space before function parentheses
return state.scanner
}
}

// @ts-ignore
const scanner = createScanner(mockInterface, {
initialDelay: 1 // allow debugging directly on macOS - if this was 0 then it would try to use requestIdleCallback, which is absent in WebKit
})
let state = {
/** @type {import('./Scanner.js').Scanner | undefined} */
scanner: undefined,
/** @type {HTMLSelectElement|null} */
list: document.querySelector('select[name="html-list"]'),

Check failure on line 54 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Unexpected trailing comma
}

const url = new URL(window.location.href);

Check failure on line 57 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
const initial = url.searchParams.get('form');

Check failure on line 58 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
const log = url.searchParams.has('log');

Check failure on line 59 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon

scanner.init()
if (log) {
sessionStorage.setItem('ddg-autofill-debug', 'true')
} else {
sessionStorage.setItem('ddg-autofill-debug', 'false')
}

loadList()
.then(() => {
if (initial) {
loadNewForm(initial).catch(console.error)
}
})


Check failure on line 74 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

More than 1 blank line not allowed
async function loadList() {

Check failure on line 75 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Missing space before function parentheses
const url = new URL(`/test-forms/index.json`, window.location.href);

Check failure on line 76 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
fetch(url)
.then(response => response.json())
.then(x => {
x.forEach(item => {
const option = document.createElement('option');

Check failure on line 81 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
option.value = item.html;

Check failure on line 82 in src/Scanner.debug.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
option.textContent = item.html;
state.list?.appendChild(option);
});
})
}
async function loadNewForm(filename) {
const url = new URL(`/test-forms/${filename}`, window.location.href);
fetch(url)
.then(response => response.text())
.then(html => {
let mainElement = document.querySelector('main');
if (mainElement) {
mainElement.innerHTML = html;
if (state.list) {
state.list.value = filename
}
state.scanner = createScanner(/** @type {any} */(mockInterface), {
initialDelay: 1 // allow debugging directly on macOS - if this was 0 then it would try to use requestIdleCallback, which is absent in WebKit
})
state.scanner?.init();
} else {
console.log("'main' element not found on the page.");
}
})
.catch(error => {
console.error('Error:', error);
});
}

document.querySelector('select[name="html-list"]')?.addEventListener('change', (e) => {
const elem = /** @type {HTMLSelectElement} */(e.target);
const next = new URL(window.location.href);
next.searchParams.set('form', elem.value);
window.location.href = next.href;
})
5 changes: 0 additions & 5 deletions src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
shouldLog, pierceShadowTree,
findEnclosedElements
} from './autofill-utils.js'
import { AddDebugFlagCall } from './deviceApiCalls/__generated__/deviceApiCalls.js'

const {
MAX_INPUTS_PER_PAGE,
Expand Down Expand Up @@ -106,10 +105,6 @@ class DefaultScanner {
* @returns {(reason: string, ...rest) => void}
*/
init () {
if (this.device.globalConfig.isExtension) {
this.device.deviceApi.notify(new AddDebugFlagCall({ flag: 'autofill' }))
}

// Add the shadow DOM listener. Handlers in handleEvent
window.addEventListener('pointerdown', this, true)
// We don't listen for focus events on mobile, they can cause keyboard flashing
Expand Down
36 changes: 29 additions & 7 deletions src/scanner-debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Scanner Test</title>
<style>
html, body {
margin: 0;
padding: 0;
}
.header {
padding: 1em;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
}
main {
padding: 1em;
}
</style>
</head>
<body>
<div>
<form action="">
<label for="email"><input type="text" id="email"></label>
<label for="password"><input type="password" id="password"></label>
<button type="submit">Sign in</button>
</form>
</div>
<script type="importmap">
{
"imports": {
"@duckduckgo/content-scope-scripts/src/apple-utils": "/node_modules/@duckduckgo/content-scope-scripts/src/apple-utils.js"
}
}
</script>
<header class="header">
<label>
Select a form:
<select name="html-list">
<option disabled selected></option>
</select>
</label>
</header>
<main><!-- Content will be loaded here --></main>
<script type="module" src="./Scanner.debug.js"></script>
</body>
</html>
16 changes: 7 additions & 9 deletions swift-package/Resources/assets/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 951e55e

Please sign in to comment.