Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailto and Default Mail Client fix #384

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@types/lodash.debounce": "^4.0.6",
"@types/node": "^14.14.34",
"@types/react-dom": "^18.0.6",
"@types/winreg": "^1.2.32",
"babel-loader": "^8.2.5",
"builder-util-runtime": "^8.7.3",
"cross-env": "^7.0.3",
Expand Down Expand Up @@ -86,6 +87,7 @@
"stylelint-config-xo": "^0.20.0",
"type-fest": "^0.21.3",
"typescript": "^4.2.3",
"winreg": "^1.2.4",
"write-json-file": "^4.3.0",
"xo": "^0.38.2"
},
Expand Down
175 changes: 147 additions & 28 deletions src/main/account-views/preload/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,42 +253,161 @@ export function initGmail() {
}
)

ipcRenderer.on('gmail:compose-mail', async (_event, to?: string) => {
clickElement('div[gh="cm"]')
ipcRenderer.on(
'gmail:compose-mail',
async (_event, mailtoString?: string) => {
if (!mailtoString) {
return
}

if (!to) {
return
}
// Decode mailto string
const url = new URL(mailtoString)

const toElement = await elementReady<HTMLTextAreaElement>(
'textarea[name="to"]',
{
stopOnDomReady: false,
timeout: 60000
const to = decodeURIComponent(url.pathname)
const parameters = new URLSearchParams(url.search)

const cc = parameters.get('cc')
const bcc = parameters.get('bcc')
const subject = parameters.get('subject')
const body = parameters.get('body')

if (!to) {
return
}
)

if (!toElement) {
return
}
clickElement('div[gh="cm"]')

toElement.focus()
toElement.value = to
const newMessageElement = await elementReady<HTMLTextAreaElement>(
'div[aria-label="New Message"]:not([aria-checked])',
{
stopOnDomReady: false,
timeout: 60000
}
)

const subjectElement = document.querySelector<HTMLInputElement>(
'input[name="subjectbox"]'
)
if (!newMessageElement) {
return
}

if (!subjectElement) {
return
}
newMessageElement.ariaChecked = 'true'

// The subject input can't be focused immediately after
// settings the "to" input value for an unknown reason.
setTimeout(() => {
subjectElement.focus()
}, 200)
})
const toDivElement = newMessageElement.querySelector<HTMLInputElement>(
'div[name="to"]'
)

if (!toDivElement) {
return
}

toDivElement.focus()
await new Promise((resolve) => {
setTimeout(resolve, 200)
})

const toElement = toDivElement.querySelector<HTMLInputElement>('input')
if (!toElement) {
return
}

toElement.focus()
toElement.value = to

if (cc) {
const ccButtonElement = await elementReady<HTMLTextAreaElement>(
'span[class="aB gQ pE"]',
{
target: newMessageElement,
stopOnDomReady: false,
timeout: 60000
}
)
if (!ccButtonElement) {
return
}

ccButtonElement.click()

const ccElement = await elementReady<HTMLTextAreaElement>(
'div[name="cc"] input',
{
target: newMessageElement,
stopOnDomReady: false,
timeout: 60000
}
)
if (!ccElement) {
return
}

ccElement.focus()
ccElement.value = cc
}

if (bcc) {
const bccButtonElement = await elementReady<HTMLTextAreaElement>(
'span[class="aB gQ pB"]',
{
target: newMessageElement,
stopOnDomReady: false,
timeout: 60000
}
)
if (!bccButtonElement) {
return
}

bccButtonElement.click()

const bccElement = await elementReady<HTMLTextAreaElement>(
'div[name="bcc"] input',
{
target: newMessageElement,
stopOnDomReady: false,
timeout: 60000
}
)
if (!bccElement) {
return
}

bccElement.focus()
bccElement.value = bcc
}

const subjectElement = newMessageElement.querySelector<HTMLInputElement>(
'input[name="subjectbox"]'
)
if (!subjectElement) {
return
}

let subjectSet = false

// The subject input can't be focused immediately after
// settings the "to" input value for an unknown reason.
setTimeout(() => {
subjectElement.focus()
if (subject) {
subjectElement.value = subject
subjectSet = true
}
}, 200)

if (body || subjectSet) {
const bodyElement = newMessageElement.querySelector<HTMLInputElement>(
'div[role="textbox"]'
)
if (!bodyElement) {
return
}

if (body) {
bodyElement.focus()
bodyElement.textContent = body
}
}
}
)

setInterval(() => {
previousNewMails.clear()
Expand Down
10 changes: 8 additions & 2 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ export async function initApp() {
app.disableHardwareAcceleration()
}

app.on('second-instance', () => {
app.on('second-instance', (_event, argv, _workingDirectory) => {
const mailtoString = argv.find((s) => s.startsWith('mailto:'))

if (mailtoString) {
sendToSelectedAccountView('gmail:compose-mail', mailtoString)
}

showMainWindow()
})

app.on('open-url', (_event, mailto) => {
sendToSelectedAccountView('gmail:compose-mail', mailto.split(':')[1])
sendToSelectedAccountView('gmail:compose-mail', mailto)

showMainWindow()
})
Expand Down
Loading