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

Fix replacing in content editors #56

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
2 changes: 1 addition & 1 deletion assets/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

</div>

<div class="text-center">Version 1.6.7</div>
<div class="text-center">Version 1.6.8</div>
</body>

</html>
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
]
}
],
"description": "Search for text in on a webpage and replace it with different text.",
"description": "Search for text on a webpage and replace it with different text.",
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCr3R0tWRUKUERxg/cghorcvKPDxba+0Ko/3Metk/0XsUTPZcU1xBQZCY441i3lK2ZkH/Td3rs/l8HvbVnkN37NiFm0QQOIJGJi7vp1GdQsrr5uZA/611TsQAWpxxHzR9N4km5wu8e/Xmw2ZG5WKfWVPtUhozEDHTk5CVgeTUOMAQIDAQAB",
"manifest_version": 3,
"name": "Search and Replace",
Expand All @@ -46,6 +46,6 @@
"https://*/*"
],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "1.6.7",
"version": "1.6.8",
"options_page": "assets/options.html"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "search_and_replace",
"version": "1.6.7",
"version": "1.6.8",
"resolutions": {
"author": "Chris Taylor <[email protected]>"
},
Expand Down
2 changes: 2 additions & 0 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ window.addEventListener('DOMContentLoaded', function () {
if (recentSearch) {
restoreSearchReplaceInstance(recentSearch)
}
// Trigger a search term count if there is an existing search term
tabQuery('store', recentSearch, history, tabQueryCallback).then((r) => console.log(r))
})
;(<HTMLButtonElement>document.querySelector('#historyHeader')).addEventListener('click', historyHeaderClickHandler)

Expand Down
59 changes: 16 additions & 43 deletions src/searchreplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,12 @@ async function cmsEditor(
} else {
const editor = <HTMLElement>document.querySelector(richTextEditor.editor.value)
const initialText = editor.textContent || ''
console.log('initial Text', initialText)
console.log('inner Text', editor.innerText)
console.log('inner HTML', editor.innerHTML)
const newText = initialText.replace(searchPattern, replaceTerm)
await replaceInContentEditableElement(window, editor, newText)
console.log('newText', newText)
await replaceInContentEditableElement(window, editor, initialText, newText)
replaced = initialText !== newText
}
} catch (err) {
Expand All @@ -347,55 +351,24 @@ async function cmsEditor(
async function replaceInContentEditableElement(
window: Window,
element: HTMLElement,
initialText: string,
replacementText: string
): Promise<boolean> {
return new Promise((resolve) => {
const dataTransfer = new DataTransfer()

// this may be 'text/html' if it's required
dataTransfer.setData('text/plain', `${replacementText}`)

// select the content editable area
element.dispatchEvent(new FocusEvent('focus', { bubbles: true }))
console.log('element.textContent', element.textContent)
if (element.innerText === element.innerHTML) {
console.log('set textContent in element', element)
element.textContent = replacementText
} else {
console.log('replacing', initialText, 'in', element.innerHTML, 'with', replacementText)
element.innerHTML = element.innerHTML.replace(initialText, replacementText)

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML

[DOM text](1) is reinterpreted as HTML without escaping meta-characters.
}

// select all the text
selectElementContents(window, element)

// delete any text in the content editable area
element.dispatchEvent(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
keyCode: 46,
shiftKey: true,
})
)

// wait a short time for the delete to happen
setTimeout(() => {
// paste the replacement text
element.dispatchEvent(
new ClipboardEvent('paste', {
clipboardData: dataTransfer,

// need these for the event to reach Draft paste handler
bubbles: true,
cancelable: true,
})
)

// clear DataTransfer Data
dataTransfer.clearData()
element.dispatchEvent(new Event('input', { bubbles: true }))

if (element.textContent !== replacementText) {
console.log('set textContent')
element.textContent = replacementText
} else {
console.log("didn't need to set textContent ")
element.dispatchEvent(new Event('input', { bubbles: true }))
}
resolve(true)
}, 100)
resolve(element.innerText !== initialText)
})
}

Expand Down
13 changes: 7 additions & 6 deletions tests/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
<!-- An HTML page containing all kinds of input fields in which we will test replacing text -->
<body>
<div>
<h1>Tests</h1>
<div>
<h1>Input Field</h1>
<h2>Input Field</h2>
<form action="#" method="post">
<input type="text" name="text" value="This is a test!!!">
<input type="submit" value="Submit">
</form>
</div>
<div>
<h1>Text Area</h1>
<h2>Text Area</h2>
<textarea name="text" rows="10" cols="50">This is a test!!!</textarea>
</div>
<div>
<h1>Text Area with HTML</h1>
<h2>Text Area with HTML</h2>
<textarea name="text" rows="10" cols="50"><p>This is a test!!!</p></textarea>
</div>
<div>
<h1>Editable div with [role="textbox"]</h1>
<h2>Editable div with [role="textbox"]</h2>
<div contenteditable="true" role="textbox">This is a test!!!</div>
</div>
<div>
<h1>Hidden input</h1>
<h2>Hidden input</h2>
<form action="#" method="post">
<input type="hidden" name="hidden" value="This is a test!!!">
</form>
</div>
<div>
<h1>Plain old div</h1>
<h2>Plain old div</h2>
<div>This is a test!!!</div>
</div>
</div>
Expand Down