-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
158 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!doctype html> | ||
<textarea id="text"></textarea> | ||
<script src="offscreen.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Once the message has been posted from the service worker, checks are made to | ||
// confirm the message type and target before proceeding. This is so that the | ||
// module can easily be adapted into existing workflows where secondary uses for | ||
// the document (or alternate offscreen documents) might be implemented. | ||
|
||
// Registering this listener when the script is first executed ensures that the | ||
// offscreen document will be able to receive messages when the promise returned | ||
// by `offscreen.createDocument()` resolves. | ||
chrome.runtime.onMessage.addListener(handleMessages); | ||
|
||
// This function performs basic filtering and error checking on messages before | ||
// dispatching the | ||
// message to a more specific message handler. | ||
async function handleMessages(message) { | ||
// Return early if this message isn't meant for the offscreen document. | ||
if (message.target !== 'offscreen-doc') { | ||
return; | ||
} | ||
|
||
// Dispatch the message to an appropriate handler. | ||
switch (message.type) { | ||
case 'copy-data-to-clipboard': | ||
handleClipboardWrite(message.data); | ||
break; | ||
default: | ||
console.warn(`Unexpected message type received: '${message.type}'.`); | ||
} | ||
} | ||
|
||
// We use a <textarea> element for two main reasons: | ||
// 1. preserve the formatting of multiline text, | ||
// 2. select the node's content using this element's `.select()` method. | ||
const textEl = document.querySelector('#text'); | ||
|
||
// Use the offscreen document's `document` interface to write a new value to the | ||
// system clipboard. | ||
// | ||
// At the time this demo was created (Jan 2023) the `navigator.clipboard` API | ||
// requires that the window is focused, but offscreen documents cannot be | ||
// focused. As such, we have to fall back to `document.execCommand()`. | ||
async function handleClipboardWrite(data) { | ||
try { | ||
// Error if we received the wrong kind of data. | ||
if (typeof data !== 'string') { | ||
throw new TypeError( | ||
`Value provided must be a 'string', got '${typeof data}'.` | ||
); | ||
} | ||
|
||
// `document.execCommand('copy')` works against the user's selection in a web | ||
// page. As such, we must insert the string we want to copy to the web page | ||
// and to select that content in the page before calling `execCommand()`. | ||
textEl.value = data; | ||
textEl.select(); | ||
document.execCommand('copy'); | ||
} finally { | ||
// Job's done! Close the offscreen document. | ||
window.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
export const copyToClipboard = (str: string) => { | ||
const el = document.createElement('input'); | ||
el.value = str; | ||
el.setAttribute('readonly', ''); | ||
el.style.display = 'absolute'; | ||
el.style.left = '-9999px'; | ||
document.body.appendChild(el); | ||
el.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(el); | ||
export const copyToClipboard = async (str: string) => { | ||
if (chrome) { | ||
// Solution 1 - As of Jan 2023, service workers cannot directly interact with | ||
// the system clipboard using either `navigator.clipboard` or | ||
// `document.execCommand()`. To work around this, we'll create an offscreen | ||
// document and pass it the data we want to write to the clipboard. | ||
await chrome.offscreen.createDocument({ | ||
url: 'offscreen.html', | ||
reasons: [chrome.offscreen.Reason.CLIPBOARD], | ||
justification: 'Write text to the clipboard.' | ||
}); | ||
|
||
// Now that we have an offscreen document, we can dispatch the | ||
// message. | ||
await chrome.runtime.sendMessage({ | ||
type: 'copy-data-to-clipboard', | ||
target: 'offscreen-doc', | ||
data: str | ||
}); | ||
} | ||
if (document) { | ||
const el = document.createElement('input'); | ||
el.value = str; | ||
el.setAttribute('readonly', ''); | ||
el.style.display = 'absolute'; | ||
el.style.left = '-9999px'; | ||
document.body.appendChild(el); | ||
el.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(el); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import { api } from '../api'; | ||
|
||
export const fillInput = async (text: string) => { | ||
console.log(text); | ||
await api.tabs.executeScript({ | ||
code: ` | ||
if (!document.querySelectorAll("input[type='password']")[0]) { | ||
alert('No he encontrado donde poner la contraseña ☹️'); | ||
} else { | ||
document.querySelectorAll("input[type='password']")[0].value = '${text}'; | ||
document.querySelectorAll("input[type='password']")[0].closest('form').submit(); | ||
}`, | ||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { | ||
if (tabs.length > 0 && tabs[0].id) { | ||
chrome.scripting.executeScript({ | ||
target: { tabId: tabs[0].id }, | ||
func: (password) => { | ||
// Injected function that fills in the password field | ||
const passwordInput: HTMLInputElement = document.querySelector("input[type='password']"); | ||
if (!passwordInput) { | ||
alert('No password field found ☹️'); | ||
} else { | ||
passwordInput.value = password; | ||
const form = passwordInput.closest('form'); | ||
if (form) { | ||
form.submit(); | ||
} | ||
} | ||
}, | ||
args: [text], // Pass the password text to the injected function | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters