fix reader page to use the background script as a clipboard source instead of breaking the selection all the time

This commit is contained in:
wareya committed 2017-12-12 19:38:11 -05:00
1 parent f9684b9f9a
commit ca9c532fb1
6 files changed
+65 -31

No files matched your search

+38 -4
View File
@@ -705,13 +705,13 @@ function build_lookup_comb (forms)
let last_lookup = "";
let last_time_lookup = Date.now();
function lookup_indirect(text, time)
function lookup_indirect(text, time, divexisted)
{
if(text == "")
return;
//if(time < last_time_lookup+20) // reduces lag buildup
// return;
if(text == last_lookup)// && (exists_div()))// || time < last_time_lookup+100)) // helps reduce double-lookups
if(text == last_lookup && divexisted)// || time < last_time_lookup+100)) // helps reduce double-lookups
return;
last_lookup = text;
last_time_lookup = time;
@@ -719,12 +719,13 @@ function lookup_indirect(text, time)
// try to look up successively shorter substrings of text
// deconjugate() returns possible deconjugations, one of which has zero deconjugations, i.e. the plain text
// build_lookup_comb looks for dictionary definitions matching any deconjugation, returning a list of them
console.log("trying to look up " + text);
let forms = deconjugate(text);
let result = build_lookup_comb(forms);
while(result === undefined && text.length > 0)
{
//console.log("trying to look up " + text);
text = text.substring(0, text.length-1);
console.log("trying to look up " + text);
forms = deconjugate(text);
result = build_lookup_comb(forms);
}
@@ -810,16 +811,49 @@ browser.contextMenus.create({
onclick: tryopenwindow
});
let origin_tab = undefined;
function set_up_paste_overload()
{
document.querySelector("#nazeka-paste-target").addEventListener("paste", (event) =>
{
// skip event if there's no plaintext; thanks github.com/kmltml/clipboard-inserter
let text = event.clipboardData.getData("text/plain");
event.preventDefault();
if(origin_tab != undefined)
browser.tabs.sendMessage(origin_tab.id, {type:"text",text:text});
});
}
if (document.readyState == "complete")
set_up_paste_overload();
else
document.addEventListener("DOMContentLoaded", set_up_paste_overload);
function clipboard_hook(tab)
{
origin_tab = tab;
let target = document.querySelector("#nazeka-paste-target");
target.textContent = "";
target.focus();
document.execCommand("paste");
}
browser.runtime.onMessage.addListener((req, sender, sendResponse) =>
{
if (req.type == "search")
{
sendResponse(lookup_indirect(req.text, req.time));
sendResponse(lookup_indirect(req.text, req.time, req.divexisted));
}
else if (req.type == "toggle")
{
toggle_enabled();
}
else if (req.type == "gimmetext")
{
clipboard_hook(sender.tab);
}
else
{
return;
+1
View File
@@ -0,0 +1 @@
<html><body><script src="background-script.js" charset="UTF-8"></script><textarea id="nazeka-paste-target"></textarea></body></html>
+1 -1
View File
@@ -11,7 +11,7 @@
],
"background": {
"scripts": ["background-script.js"]
"page": "background.html"
},
"web_accessible_resources": [
+1 -1
View File
@@ -14,7 +14,7 @@ html
overflow-y: scroll;
}
</style>
<script src="reader.js"></script>
<script src="reader.js" charset="UTF-8"></script>
</head>
<body lang="ja">
+23 -24
View File
@@ -1,3 +1,7 @@
// Copyright 2017; Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
'use strict';
function toggle()
{
browser.runtime.sendMessage({type:"toggle"});
@@ -38,37 +42,32 @@ function cycle_text(text)
text_previous = text;
}
window.addEventListener("load", () =>
function gimmetext()
{
document.querySelector("#nazeka-paste-target").addEventListener("paste", (event) =>
{
// skip event if there's no plaintext; thanks github.com/kmltml/clipboard-inserter
let text = event.clipboardData.getData("text/plain");
if(text !== "")
cycle_text(text);
event.preventDefault();
})
});
function checkpaste()
{
let target = document.querySelector("#nazeka-paste-target");
target.parentNode.style.visibility = "visible";
target.textContent = "";
target.focus();
document.execCommand("paste");
target.parentNode.style.visibility = "hidden";
browser.runtime.sendMessage({type:"gimmetext"});
}
let interval = 250;
async function checkpaste()
{
try
{
let text = await gimmetext();
cycle_text(text);
}
catch(error){}
setInterval(checkpaste, interval);
setTimeout(checkpaste, interval);
}
setTimeout(checkpaste, interval);
var executing = browser.tabs.executeScript({
file: "/texthook.js",
allFrames: true
});
browser.runtime.onMessage.addListener((req, sender, sendResponse) =>
{
if (req.type == "text")
cycle_text(req.text);
});
+1 -1
View File
@@ -518,7 +518,7 @@ async function lookup_loop()
//console.log("queue not empty");
let lookup = lookup_queue.pop();
lookup_queue = [];
let response = await browser.runtime.sendMessage({type:"search", text:lookup[0], time:Date.now()});
let response = await browser.runtime.sendMessage({type:"search", text:lookup[0], time:Date.now(), divexisted:exists_div()});
if(response)
{
let mydiv = build_div(response.text, response.result);