Skip to content

Commit

Permalink
feat: pasteIntoPostBox on twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Misaka-0x447f committed Sep 19, 2019
1 parent 5c570a3 commit 1740ab8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function pasteIntoPostBoxFacebook(
console.log('Awaiting dialog')
}
try {
const [element] = activated.evaluateOnce()
const [element] = activated.evaluate()
element.focus()
await sleep(100)
if ('value' in document.activeElement!) dispatchCustomEvents('input', text)
Expand Down
19 changes: 10 additions & 9 deletions src/social-network-provider/twitter.com/ui/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { dispatchCustomEvents, sleep, untilDocumentReady } from '../../../utils/utils'
import { editProfileButtonSelector, editProfileTextareaSelector, newPostEditorInnerSelector } from '../utils/selector'
import { editProfileButtonSelector, editProfileTextareaSelector } from '../utils/selector'
import { geti18nString } from '../../../utils/i18n'
import { SocialNetworkUITasks } from '../../../social-network/ui'
import { SocialNetworkUI, SocialNetworkUITasks } from '../../../social-network/ui'
import { fetchBioCard, fetchPost } from '../utils/status'
import { resolveInfoFromBioCard } from '../utils/fetch'
import { getFocus, getText } from '../utils/postBox'

const taskPasteIntoPostBox = async (text: string) => {
await untilDocumentReady()
const i = newPostEditorInnerSelector().evaluate()!
i.click()
dispatchCustomEvents('input', text)
throw new Error('Logic not complete for now, remove this before next release')
// TODO: detect if things successfully paste in by something like, innerText
const taskPasteIntoPostBox: SocialNetworkUI['taskPasteIntoPostBox'] = async (text, opt) => {
await getFocus() // This also waits for document loaded
dispatchCustomEvents('paste', text)
if (getText() !== text) {
console.warn('Text pasting failed')
prompt(opt.warningText, text)
}
}

const taskPasteIntoBio = async (text: string) => {
Expand Down
33 changes: 33 additions & 0 deletions src/social-network-provider/twitter.com/utils/postBox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { sleep, untilDocumentReady } from '../../../utils/utils'
import { isUndefined } from 'lodash-es'
import { newPostEditorFocusAnchor, newPostEditorHasFocus, newPostEditorSelector } from './selector'
import { equal } from '../../../utils/assert'

export const getFocus = async () => {
await untilDocumentReady()
while (isUndefined(newPostEditorSelector().evaluate())) {
await sleep(1000)
}

if (!hasFocus()) {
try {
const i = newPostEditorFocusAnchor().evaluate()!
i.focus()
} catch {
await requestManualClick()
}
}

equal(hasFocus(), true, 'Failed to get focus')
}

const hasFocus = () => !isUndefined(newPostEditorHasFocus().evaluate())

const requestManualClick = async () => {
alert('Please manually select the post box')
while (!hasFocus()) {
await sleep(1000)
}
}

export const getText = () => newPostEditorSelector().evaluate()!.innerText
6 changes: 4 additions & 2 deletions src/social-network-provider/twitter.com/utils/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export const postViewMain = () =>
const newPostEditor = '[role="main"] [role="progressbar"] ~ div'
export const newPostEditorBelow = () => querySelector<HTMLDivElement>(`${newPostEditor} > div`)
export const newPostEditorSelector = () => querySelector<HTMLDivElement>(`${newPostEditor} .DraftEditor-root`)
export const newPostEditorInnerSelector = () =>
newPostEditorSelector().querySelector<HTMLDivElement>('.DraftEditor-editorContainer > div')
export const newPostEditorFocusAnchor = () =>
querySelector<HTMLDivElement>(`${newPostEditor} .public-DraftEditor-content`)

export const newPostEditorHasFocus = () => querySelector(`.public-DraftEditorPlaceholder-hasFocus`)

export const editProfileButtonSelector = () =>
querySelector<HTMLAnchorElement>('[data-testid="primaryColumn"] [href="/settings/profile"]')
Expand Down

0 comments on commit 1740ab8

Please sign in to comment.