Skip to content

Commit

Permalink
add some more reader options
Browse files Browse the repository at this point in the history
  • Loading branch information
wareya committed Jun 15, 2019
1 parent 5793cdc commit dc42b59
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
8 changes: 8 additions & 0 deletions background-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,14 @@ browser.runtime.onMessage.addListener((req, sender) =>
{
refresh_json();
}
else if (req.type == "reader_lookup")
{
browser.tabs.sendMessage(req.id, {type:req.type, x:req.x, y:req.y, text:req.text});
}
else if (req.type == "reader_mode")
{
browser.tabs.sendMessage(req.id, {type:req.type});
}
else if (req.type == "ankiconnect_mine")
{
console.log("got request");
Expand Down
24 changes: 18 additions & 6 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,6 @@ function defaults()
default: 300,
label: "Reader window default height"
});
settings.push({
id: "reader_reverse",
kind: "checkbox",
default: false,
label: "Reader insert at bottom"
});
settings.push({
id: "reader_leeway",
kind: "number",
Expand All @@ -379,6 +373,24 @@ function defaults()
default: 200,
label: "Reader autoscroll leeway (when \"insert at bottom\" is enabled) (0 disables)"
});
settings.push({
id: "reader_sticky",
kind: "checkbox",
default: false,
label: "Force 'sticky' mode in reader"
});
settings.push({
id: "reader_auto",
kind: "checkbox",
default: false,
label: "Automatically look up the beginning of new text in the reader"
});
settings.push({
id: "reader_reverse",
kind: "checkbox",
default: false,
label: "Reader insert at bottom"
});

settings.push({
id: "reader_bg",
Expand Down
27 changes: 26 additions & 1 deletion reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ reader_right_padding: "0px",
reader_max_width: "1000px",
reader_margin: "8px",
reader_font: "",
reader_auto: false
};

function update_styles()
Expand Down Expand Up @@ -49,6 +50,7 @@ async function reader_settings_init()
getvar("reader_max_width", "1000px");
getvar("reader_margin", "8px");
getvar("reader_font", "");
getvar("reader_auto", false);
} catch(err) {} // options not stored yet
}

Expand Down Expand Up @@ -102,6 +104,25 @@ function reader_update(text)
}

document.getElementById("linecount").innerText = parseInt(document.getElementById("linecount").innerText)+1;

let x = newnode.getBoundingClientRect().x - document.documentElement.getBoundingClientRect().x;
let y = newnode.getBoundingClientRect().y - document.documentElement.getBoundingClientRect().y;
y += Math.min(5, newnode.getBoundingClientRect().height/2);

if(reader_settings.reader_auto)
send_lookup_request(x, y, text);
}

async function send_lookup_request(x, y, text)
{
let id = (await browser.tabs.getCurrent()).id;
browser.runtime.sendMessage({id:id, type:"reader_lookup", x:x, y:y, text:text});
}

async function send_reader_mode(x, y, text)
{
let id = (await browser.tabs.getCurrent()).id;
browser.runtime.sendMessage({id:id, type:"reader_mode"});
}

let reader_text_previous = "";
Expand Down Expand Up @@ -141,4 +162,8 @@ browser.runtime.onMessage.addListener((req, sender) =>
return Promise.resolve(undefined);
});

window.onload = update_styles;
window.onload = () =>
{
send_reader_mode();
update_styles();
}
31 changes: 25 additions & 6 deletions texthook.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ disableborder: false,
space_saver: false,
hide_deconj: false,
showoriginal: true,
reader_sticky: false,
definitions_mode: 0,
normal_definitions_in_mining: false,
alternatives_mode: 3, // 0: longest only; 1: longest and shortest; 2: longest and second longest; 3: all matches
Expand Down Expand Up @@ -60,6 +61,7 @@ only_selection: false
};

let platform = "win";
let is_reader = false;

async function get_real_platform()
{
Expand Down Expand Up @@ -184,6 +186,11 @@ function getViewportSize(mydoc)
return { w: mydoc.body.clientWidth, h: mydoc.body.clientHeight };
}

function is_sticky()
{
return (settings.sticky && platform != "android" && !mining_ui_exists()) || (is_reader && settings.reader_sticky);
}

// here we set all the styling and positioning of the div, passing "middle" as the actual contents of it.
// this is rather elaborate because of 1) a lack of shadow DOM, even for just styling 2) options 3) """features""" of how HTML viewport stuff works that are actually terrible
let last_display_x = 0;
Expand Down Expand Up @@ -295,7 +302,7 @@ function display_div(middle, x, y)

let corner = settings.corner;

if(settings.sticky && platform != "android" && !mining_ui_exists())
if(is_sticky())
set_sticky_styles(outer);
else
{
Expand Down Expand Up @@ -600,7 +607,7 @@ function build_div_inner(text, result, moreText, index, first_of_many = false)
original.appendChild(original_inner);
original.appendChild(document.createTextNode(moreText_end));

if(first_of_many && (platform == "android" || (settings.sticky && !mining_ui_exists())))
if(first_of_many && is_sticky())
{
let buttons = document.createElement("div");
let left_arrow = document.createElement("img");
Expand Down Expand Up @@ -1205,6 +1212,7 @@ async function settings_init()
getvar("space_saver", false);
getvar("hide_deconj", false);
getvar("showoriginal", true);
getvar("reader_sticky", false);

getvar("bgcolor", "#111111");
getvar("fgcolor", "#CCCCCC");
Expand Down Expand Up @@ -1344,7 +1352,7 @@ function lookup_enqueue(text, x, y, x2, y2, moreText, index)

function lookup_cancel()
{
if(!settings.sticky || mining_ui_exists() || platform == "android")
if(!is_sticky())
delete_div();
}

Expand Down Expand Up @@ -1713,7 +1721,7 @@ function update(event)

if(!settings.enabled) return;

if(settings.popup_follows_mouse && exists_div() && platform != "android" && (!settings.sticky || mining_ui_exists()))
if(settings.popup_follows_mouse && exists_div() && !is_sticky())
{
let other = get_div();
//let middle = other.firstChild.cloneNode(true);
Expand Down Expand Up @@ -1787,7 +1795,7 @@ function update(event)
hitrect = range.getBoundingClientRect();
}
// sticky mode and android need to break out on parent detection
if(ele && !ele.contains(textNode) && platform != "android" && (!settings.sticky || mining_ui_exists()))
if(ele && !ele.contains(textNode) && is_sticky())
{
textNode = undefined;
offset = undefined;
Expand All @@ -1808,7 +1816,7 @@ function update(event)
hitpage(event.clientX, 0, event.clientY);
}

if ((platform == "android" || (settings.sticky && !mining_ui_exists())) && exists_div())
if (exists_div() && is_sticky())
{
let ele = get_div();
if(ele.contains(textNode))
Expand Down Expand Up @@ -2277,6 +2285,17 @@ function keyuntest(event)

}

browser.runtime.onMessage.addListener((req, sender) =>
{
if(req.type == "reader_lookup")
send_lookup([req.text, req.x, req.y, req.x, req.y, req.text, 0]);
else if(req.type == "reader_mode")
{
console.log("we a readuh!");
is_reader = true;
}
});

window.addEventListener("mousemove", update);
window.addEventListener("keydown", keytest);
window.addEventListener("keyup", keyuntest);
Expand Down

0 comments on commit dc42b59

Please sign in to comment.