add workaround to Chrome clipboard.readText

For some reason this doesn't work on Firefox, so keep using the new API
for Firefox
This commit is contained in:
siikamiika 2019-10-27 15:46:27 +02:00
parent d3f51690f8
commit 48776145d6
5 changed files with 31 additions and 2 deletions

View File

@ -5,6 +5,8 @@
<meta name="viewport" content="width=device-width,initial-scale=1" /> <meta name="viewport" content="width=device-width,initial-scale=1" />
</head> </head>
<body> <body>
<div id="clipboard-paste-target" contenteditable="true"></div>
<script src="/mixed/lib/dexie.min.js"></script> <script src="/mixed/lib/dexie.min.js"></script>
<script src="/mixed/lib/handlebars.min.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/mixed/lib/jszip.min.js"></script> <script src="/mixed/lib/jszip.min.js"></script>

View File

@ -401,3 +401,11 @@ async function apiFocusTab(tab) {
// Edge throws exception for no reason here. // Edge throws exception for no reason here.
} }
} }
async function apiClipboardGet() {
const clipboardPasteTarget = utilBackend().clipboardPasteTarget;
clipboardPasteTarget.innerText = '';
clipboardPasteTarget.focus();
document.execCommand('paste');
return clipboardPasteTarget.innerText;
}

View File

@ -30,6 +30,8 @@ class Backend {
this.isPreparedResolve = null; this.isPreparedResolve = null;
this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve));
this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
this.apiForwarder = new BackendApiForwarder(); this.apiForwarder = new BackendApiForwarder();
} }
@ -187,7 +189,8 @@ Backend.messageHandlers = {
forward: ({action, params}, sender) => apiForward(action, params, sender), forward: ({action, params}, sender) => apiForward(action, params, sender),
frameInformationGet: (params, sender) => apiFrameInformationGet(sender), frameInformationGet: (params, sender) => apiFrameInformationGet(sender),
injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender), injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender),
getEnvironmentInfo: () => apiGetEnvironmentInfo() getEnvironmentInfo: () => apiGetEnvironmentInfo(),
clipboardGet: () => apiClipboardGet()
}; };
window.yomichan_backend = new Backend(); window.yomichan_backend = new Backend();

View File

@ -17,6 +17,12 @@
*/ */
let IS_FIREFOX = null;
(async () => {
const {browser} = await apiGetEnvironmentInfo();
IS_FIREFOX = ['firefox', 'firefox-mobile'].includes(browser);
})();
class DisplaySearch extends Display { class DisplaySearch extends Display {
constructor() { constructor() {
super(document.querySelector('#spinner'), document.querySelector('#content')); super(document.querySelector('#spinner'), document.querySelector('#content'));
@ -235,7 +241,13 @@ class DisplaySearch extends Display {
startClipboardMonitor() { startClipboardMonitor() {
this.clipboardMonitorIntervalId = setInterval(async () => { this.clipboardMonitorIntervalId = setInterval(async () => {
const curText = (await navigator.clipboard.readText()).trim(); let curText = null;
// TODO get rid of this and figure out why apiClipboardGet doesn't work on Firefox
if (IS_FIREFOX) {
curText = (await navigator.clipboard.readText()).trim();
} else if (IS_FIREFOX === false) {
curText = (await apiClipboardGet()).trim();
}
if (curText && (curText !== this.clipboardPrevText)) { if (curText && (curText !== this.clipboardPrevText)) {
if (this.isWanakanaEnabled()) { if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(curText); this.query.value = window.wanakana.toKana(curText);

View File

@ -72,3 +72,7 @@ function apiInjectStylesheet(css) {
function apiGetEnvironmentInfo() { function apiGetEnvironmentInfo() {
return utilInvoke('getEnvironmentInfo'); return utilInvoke('getEnvironmentInfo');
} }
function apiClipboardGet() {
return utilInvoke('clipboardGet');
}