Skip to content

Commit

Permalink
Tasks.pasteIntoPostBox done
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed May 7, 2019
1 parent f4076aa commit 967f035
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
20 changes: 5 additions & 15 deletions src/components/InjectedComponents/AdditionalPostBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { useAsync } from '../../utils/components/AsyncComponent'
import { Person } from '../../extension/background-script/PeopleService'
import { usePeople } from '../DataSource/PeopleRef'
import { SelectPeopleUI } from './SelectPeople'
import { sleep, dispatchCustomEvents, selectElementContents } from '../../utils/utils'
import { myUsername, getUsername } from '../../extension/content-script/injections/LiveSelectors'
import { useRef } from 'react'
import { useCapturedInput } from '../../utils/hooks/useCapturedEvents'
import { Avatar } from '../../utils/components/Avatar'
import Services from '../../extension/service'
import { pasteIntoPostBox } from '../../extension/content-script/tasks'

interface Props {
people: Person[]
Expand Down Expand Up @@ -98,20 +98,10 @@ export function AdditionalPostBox() {
const onRequestPost = React.useCallback(async (people, text) => {
const [encrypted, token] = await Services.Crypto.encryptTo(text, people)
const fullPost = 'Decrypt this post with ' + encrypted
const element = document.querySelector<HTMLDivElement>('.notranslate')!
element.focus()
await sleep(100)
selectElementContents(element)
await sleep(100)
dispatchCustomEvents('paste', fullPost)
// Prevent Custom Paste failed, this will cause service not available to user.
if (!element.innerText.match('🎼')) {
console.warn('Text not pasted to the text area')
navigator.clipboard.writeText(fullPost)
alert(
'Encrypted text has been copied into the clipboard!\nHowever, you need to paste it to the post box by yourself.',
)
}
pasteIntoPostBox(
fullPost,
'Encrypted text has been copied into the clipboard!\nHowever, you need to paste it to the post box by yourself.',
)
Services.Crypto.publishPostAESKey(token)
}, [])
if (!username) {
Expand Down
36 changes: 34 additions & 2 deletions src/extension/content-script/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import { AutomatedTabTask, LiveSelector, MutationObserverWatcher } from '@holoflows/kit'
import { sleep, dispatchCustomEvents } from '../../utils/utils'
import { sleep, dispatchCustomEvents, selectElementContents } from '../../utils/utils'

const bioCard = new LiveSelector().querySelector<HTMLDivElement>('#profile_timeline_intro_card')
/**
* Access: https://www.facebook.com/
* @param text
*/
export async function pasteIntoPostBox(text: string, warningText: string) {
const scrolling = document.scrollingElement || document.documentElement
const scroll = (top => () => scrolling.scroll({ top }))(scrolling.scrollTop)

const notActivated = new LiveSelector().querySelector(`[role="region"]`).querySelector('textarea')
const activated = new LiveSelector().querySelector<HTMLDivElement>('.notranslate')
// If page is just loaded
if (!activated.evaluateOnce()[0]) {
const [dom] = await new MutationObserverWatcher(notActivated).once()
dom.click()
await sleep(1500)
await new MutationObserverWatcher(activated.clone().unstable_closest(`[role="dialog"]`)).once()
}
const [element] = activated.evaluateOnce()
element.focus()
await sleep(100)
dispatchCustomEvents('paste', text)
await sleep(400)
// Prevent Custom Paste failed, this will cause service not available to user.
if (element.innerText.indexOf(text) === -1) {
console.warn('Text not pasted to the text area')
navigator.clipboard.writeText(text)
alert(warningText)
}

scroll()
}
export default AutomatedTabTask(
{
/**
Expand All @@ -25,7 +56,7 @@ export default AutomatedTabTask(
* Access: https://www.facebook.com/profile.php?id=${userId}
* Or: https://www.facebook.com/${username}?fref=pymk
*/
async verifyBio(text: string) {
async pasteIntoBio(text: string) {
const [bioEditButton] = await new MutationObserverWatcher(
bioCard.clone().querySelector<HTMLAnchorElement>('[data-tooltip-content][href="#"]'),
).once()
Expand All @@ -37,6 +68,7 @@ export default AutomatedTabTask(
input.focus()
dispatchCustomEvents('input', input.value + text)
},
pasteIntoPostBox,
},
{ memorable: true },
)!
7 changes: 5 additions & 2 deletions src/extension/injected-script/addEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export interface CustomEvents {
return key in store
}

function getEvent<T extends Event>(x: T) {
function getEvent<T extends Event>(x: T, mocks: Partial<T> = {}) {
const mockTable = {
target: document.activeElement,
srcElement: document.activeElement,
// Since it is bubbled to the document.
currentTarget: document,
// ! Why?
_inherits_from_prototype: true,
...mocks,
}
return new Proxy(x, {
get(target: T, key: keyof T) {
Expand All @@ -32,7 +34,8 @@ export interface CustomEvents {
const transfer = new DataTransfer()
transfer.setData('text/plain', text)
const e = new ClipboardEvent('paste', { clipboardData: transfer })
return getEvent(e)
// ! Why?
return getEvent(e, { defaultPrevented: false, preventDefault() {} })
},
input(text) {
// Cause react hooks the input.value getter & setter
Expand Down

0 comments on commit 967f035

Please sign in to comment.