Merge remote-tracking branch 'origin/pr/3225'

This commit is contained in:
Florian Bruhin 2017-11-02 19:43:04 +01:00
commit 1cf66976d2
5 changed files with 89 additions and 91 deletions

View File

@ -2,7 +2,7 @@ env:
browser: true browser: true
parserOptions: parserOptions:
ecmaVersion: 3 ecmaVersion: 6
extends: extends:
"eslint:all" "eslint:all"
@ -13,7 +13,6 @@ rules:
padded-blocks: ["error", "never"] padded-blocks: ["error", "never"]
space-before-function-paren: ["error", "never"] space-before-function-paren: ["error", "never"]
no-underscore-dangle: "off" no-underscore-dangle: "off"
no-var: "off"
vars-on-top: "off" vars-on-top: "off"
newline-after-var: "off" newline-after-var: "off"
camelcase: "off" camelcase: "off"

View File

@ -21,22 +21,22 @@
window.loadHistory = (function() { window.loadHistory = (function() {
// Date of last seen item. // Date of last seen item.
var lastItemDate = null; let lastItemDate = null;
// Each request for new items includes the time of the last item and an // Each request for new items includes the time of the last item and an
// offset. The offset is equal to the number of items from the previous // offset. The offset is equal to the number of items from the previous
// request that had time=nextTime, and causes the next request to skip // request that had time=nextTime, and causes the next request to skip
// those items to avoid duplicates. // those items to avoid duplicates.
var nextTime = null; let nextTime = null;
var nextOffset = 0; let nextOffset = 0;
// The URL to fetch data from. // The URL to fetch data from.
var DATA_URL = "qute://history/data"; const DATA_URL = "qute://history/data";
// Various fixed elements // Various fixed elements
var EOF_MESSAGE = document.getElementById("eof"); const EOF_MESSAGE = document.getElementById("eof");
var LOAD_LINK = document.getElementById("load"); const LOAD_LINK = document.getElementById("load");
var HIST_CONTAINER = document.getElementById("hist-container"); const HIST_CONTAINER = document.getElementById("hist-container");
/** /**
* Finds or creates the session table>tbody to which item with given date * Finds or creates the session table>tbody to which item with given date
@ -47,17 +47,17 @@ window.loadHistory = (function() {
*/ */
function getSessionNode(date) { function getSessionNode(date) {
// Find/create table // Find/create table
var tableId = ["hist", date.getDate(), date.getMonth(), const tableId = ["hist", date.getDate(), date.getMonth(),
date.getYear()].join("-"); date.getYear()].join("-");
var table = document.getElementById(tableId); let table = document.getElementById(tableId);
if (table === null) { if (table === null) {
table = document.createElement("table"); table = document.createElement("table");
table.id = tableId; table.id = tableId;
// Caption contains human-readable date // Caption contains human-readable date
var caption = document.createElement("caption"); const caption = document.createElement("caption");
caption.className = "date"; caption.className = "date";
var options = { const options = {
"weekday": "long", "weekday": "long",
"year": "numeric", "year": "numeric",
"month": "long", "month": "long",
@ -71,7 +71,7 @@ window.loadHistory = (function() {
} }
// Find/create tbody // Find/create tbody
var tbody = table.lastChild; let tbody = table.lastChild;
if (tbody.tagName !== "TBODY") { if (tbody.tagName !== "TBODY") {
tbody = document.createElement("tbody"); tbody = document.createElement("tbody");
table.appendChild(tbody); table.appendChild(tbody);
@ -80,10 +80,10 @@ window.loadHistory = (function() {
// Create session-separator and new tbody if necessary // Create session-separator and new tbody if necessary
if (tbody.lastChild !== null && lastItemDate !== null && if (tbody.lastChild !== null && lastItemDate !== null &&
window.GAP_INTERVAL > 0) { window.GAP_INTERVAL > 0) {
var interval = lastItemDate.getTime() - date.getTime(); const interval = lastItemDate.getTime() - date.getTime();
if (interval > window.GAP_INTERVAL) { if (interval > window.GAP_INTERVAL) {
// Add session-separator // Add session-separator
var sessionSeparator = document.createElement("td"); const sessionSeparator = document.createElement("td");
sessionSeparator.className = "session-separator"; sessionSeparator.className = "session-separator";
sessionSeparator.colSpan = 2; sessionSeparator.colSpan = 2;
sessionSeparator.innerHTML = "§"; sessionSeparator.innerHTML = "§";
@ -108,20 +108,20 @@ window.loadHistory = (function() {
* @returns {Element} the completed tr. * @returns {Element} the completed tr.
*/ */
function makeHistoryRow(itemUrl, itemTitle, itemTime) { function makeHistoryRow(itemUrl, itemTitle, itemTime) {
var row = document.createElement("tr"); const row = document.createElement("tr");
var title = document.createElement("td"); const title = document.createElement("td");
title.className = "title"; title.className = "title";
var link = document.createElement("a"); const link = document.createElement("a");
link.href = itemUrl; link.href = itemUrl;
link.innerHTML = itemTitle; link.innerHTML = itemTitle;
var host = document.createElement("span"); const host = document.createElement("span");
host.className = "hostname"; host.className = "hostname";
host.innerHTML = link.hostname; host.innerHTML = link.hostname;
title.appendChild(link); title.appendChild(link);
title.appendChild(host); title.appendChild(host);
var time = document.createElement("td"); const time = document.createElement("td");
time.className = "time"; time.className = "time";
time.innerHTML = itemTime; time.innerHTML = itemTime;
@ -139,11 +139,11 @@ window.loadHistory = (function() {
* @returns {void} * @returns {void}
*/ */
function getJSON(url, callback) { function getJSON(url, callback) {
var xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("GET", url, true); xhr.open("GET", url, true);
xhr.responseType = "json"; xhr.responseType = "json";
xhr.onload = function() { xhr.onload = () => {
var status = xhr.status; const status = xhr.status;
callback(status, xhr.response); callback(status, xhr.response);
}; };
xhr.send(); xhr.send();
@ -172,10 +172,10 @@ window.loadHistory = (function() {
nextTime = history[history.length - 1].time; nextTime = history[history.length - 1].time;
nextOffset = 0; nextOffset = 0;
for (var i = 0, len = history.length; i < len; i++) { for (let i = 0, len = history.length; i < len; i++) {
var item = history[i]; const item = history[i];
// python's time.time returns seconds, but js Date expects ms // python's time.time returns seconds, but js Date expects ms
var currentItemDate = new Date(item.time * 1000); const currentItemDate = new Date(item.time * 1000);
getSessionNode(currentItemDate).appendChild(makeHistoryRow( getSessionNode(currentItemDate).appendChild(makeHistoryRow(
item.url, item.title, currentItemDate.toLocaleTimeString() item.url, item.title, currentItemDate.toLocaleTimeString()
)); ));
@ -191,7 +191,7 @@ window.loadHistory = (function() {
* @return {void} * @return {void}
*/ */
function loadHistory() { function loadHistory() {
var url = DATA_URL.concat("?offset=", nextOffset.toString()); let url = DATA_URL.concat("?offset=", nextOffset.toString());
if (nextTime === null) { if (nextTime === null) {
getJSON(url, receiveHistory); getJSON(url, receiveHistory);
} else { } else {

View File

@ -27,16 +27,15 @@
*/ */
"use strict"; "use strict";
(function() { (function() {
// FIXME:qtwebengine integrate this with other window._qutebrowser code? // FIXME:qtwebengine integrate this with other window._qutebrowser code?
function isElementInViewport(node) { // eslint-disable-line complexity function isElementInViewport(node) { // eslint-disable-line complexity
var i; let i;
var boundingRect = (node.getClientRects()[0] || let boundingRect = (node.getClientRects()[0] ||
node.getBoundingClientRect()); node.getBoundingClientRect());
if (boundingRect.width <= 1 && boundingRect.height <= 1) { if (boundingRect.width <= 1 && boundingRect.height <= 1) {
var rects = node.getClientRects(); const rects = node.getClientRects();
for (i = 0; i < rects.length; i++) { for (i = 0; i < rects.length; i++) {
if (rects[i].width > rects[0].height && if (rects[i].width > rects[0].height &&
rects[i].height > rects[0].height) { rects[i].height > rects[0].height) {
@ -51,8 +50,8 @@
return null; return null;
} }
if (boundingRect.width <= 1 || boundingRect.height <= 1) { if (boundingRect.width <= 1 || boundingRect.height <= 1) {
var children = node.children; const children = node.children;
var visibleChildNode = false; let visibleChildNode = false;
for (i = 0; i < children.length; ++i) { for (i = 0; i < children.length; ++i) {
boundingRect = (children[i].getClientRects()[0] || boundingRect = (children[i].getClientRects()[0] ||
children[i].getBoundingClientRect()); children[i].getBoundingClientRect());
@ -69,7 +68,7 @@
boundingRect.left + boundingRect.width < -10) { boundingRect.left + boundingRect.width < -10) {
return null; return null;
} }
var computedStyle = window.getComputedStyle(node, null); const computedStyle = window.getComputedStyle(node, null);
if (computedStyle.visibility !== "visible" || if (computedStyle.visibility !== "visible" ||
computedStyle.display === "none" || computedStyle.display === "none" ||
node.hasAttribute("disabled") || node.hasAttribute("disabled") ||
@ -81,27 +80,27 @@
} }
function positionCaret() { function positionCaret() {
var walker = document.createTreeWalker(document.body, 4, null); const walker = document.createTreeWalker(document.body, 4, null);
var node; let node;
var textNodes = []; const textNodes = [];
var el; let el;
while ((node = walker.nextNode())) { while ((node = walker.nextNode())) {
if (node.nodeType === 3 && node.data.trim() !== "") { if (node.nodeType === 3 && node.data.trim() !== "") {
textNodes.push(node); textNodes.push(node);
} }
} }
for (var i = 0; i < textNodes.length; i++) { for (let i = 0; i < textNodes.length; i++) {
var element = textNodes[i].parentElement; const element = textNodes[i].parentElement;
if (isElementInViewport(element.parentElement)) { if (isElementInViewport(element.parentElement)) {
el = element; el = element;
break; break;
} }
} }
if (el !== undefined) { if (el !== undefined) {
var range = document.createRange(); const range = document.createRange();
range.setStart(el, 0); range.setStart(el, 0);
range.setEnd(el, 0); range.setEnd(el, 0);
var sel = window.getSelection(); const sel = window.getSelection();
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(range); sel.addRange(range);
} }

View File

@ -20,19 +20,19 @@
"use strict"; "use strict";
window._qutebrowser.scroll = (function() { window._qutebrowser.scroll = (function() {
var funcs = {}; const funcs = {};
funcs.to_perc = function(x, y) { funcs.to_perc = (x, y) => {
var x_px = window.scrollX; let x_px = window.scrollX;
var y_px = window.scrollY; let y_px = window.scrollY;
var width = Math.max( const width = Math.max(
document.body.scrollWidth, document.body.scrollWidth,
document.body.offsetWidth, document.body.offsetWidth,
document.documentElement.scrollWidth, document.documentElement.scrollWidth,
document.documentElement.offsetWidth document.documentElement.offsetWidth
); );
var height = Math.max( const height = Math.max(
document.body.scrollHeight, document.body.scrollHeight,
document.body.offsetHeight, document.body.offsetHeight,
document.documentElement.scrollHeight, document.documentElement.scrollHeight,
@ -65,9 +65,9 @@ window._qutebrowser.scroll = (function() {
window.scroll(x_px, y_px); window.scroll(x_px, y_px);
}; };
funcs.delta_page = function(x, y) { funcs.delta_page = (x, y) => {
var dx = window.innerWidth * x; const dx = window.innerWidth * x;
var dy = window.innerHeight * y; const dy = window.innerHeight * y;
window.scrollBy(dx, dy); window.scrollBy(dx, dy);
}; };

View File

@ -37,19 +37,19 @@
"use strict"; "use strict";
window._qutebrowser.webelem = (function() { window._qutebrowser.webelem = (function() {
var funcs = {}; const funcs = {};
var elements = []; const elements = [];
function serialize_elem(elem) { function serialize_elem(elem) {
if (!elem) { if (!elem) {
return null; return null;
} }
var id = elements.length; const id = elements.length;
elements[id] = elem; elements[id] = elem;
// InvalidStateError will be thrown if elem doesn't have selectionStart // InvalidStateError will be thrown if elem doesn't have selectionStart
var caret_position = 0; let caret_position = 0;
try { try {
caret_position = elem.selectionStart; caret_position = elem.selectionStart;
} catch (err) { } catch (err) {
@ -62,7 +62,7 @@ window._qutebrowser.webelem = (function() {
} }
} }
var out = { const out = {
"id": id, "id": id,
"value": elem.value, "value": elem.value,
"outer_xml": elem.outerHTML, "outer_xml": elem.outerHTML,
@ -92,16 +92,16 @@ window._qutebrowser.webelem = (function() {
out.text = elem.text; out.text = elem.text;
} // else: don't add the text at all } // else: don't add the text at all
var attributes = {}; const attributes = {};
for (var i = 0; i < elem.attributes.length; ++i) { for (let i = 0; i < elem.attributes.length; ++i) {
var attr = elem.attributes[i]; const attr = elem.attributes[i];
attributes[attr.name] = attr.value; attributes[attr.name] = attr.value;
} }
out.attributes = attributes; out.attributes = attributes;
var client_rects = elem.getClientRects(); const client_rects = elem.getClientRects();
for (var k = 0; k < client_rects.length; ++k) { for (let k = 0; k < client_rects.length; ++k) {
var rect = client_rects[k]; const rect = client_rects[k];
out.rects.push({ out.rects.push({
"top": rect.top, "top": rect.top,
"right": rect.right, "right": rect.right,
@ -126,8 +126,8 @@ window._qutebrowser.webelem = (function() {
// the cVim implementation here? // the cVim implementation here?
// https://github.com/1995eaton/chromium-vim/blob/1.2.85/content_scripts/dom.js#L74-L134 // https://github.com/1995eaton/chromium-vim/blob/1.2.85/content_scripts/dom.js#L74-L134
var win = elem.ownerDocument.defaultView; const win = elem.ownerDocument.defaultView;
var rect = elem.getBoundingClientRect(); let rect = elem.getBoundingClientRect();
if (!rect || if (!rect ||
rect.top > window.innerHeight || rect.top > window.innerHeight ||
@ -142,7 +142,7 @@ window._qutebrowser.webelem = (function() {
return false; return false;
} }
var style = win.getComputedStyle(elem, null); const style = win.getComputedStyle(elem, null);
if (style.getPropertyValue("visibility") !== "visible" || if (style.getPropertyValue("visibility") !== "visible" ||
style.getPropertyValue("display") === "none" || style.getPropertyValue("display") === "none" ||
style.getPropertyValue("opacity") === "0") { style.getPropertyValue("opacity") === "0") {
@ -159,11 +159,11 @@ window._qutebrowser.webelem = (function() {
return true; return true;
} }
funcs.find_css = function(selector, only_visible) { funcs.find_css = (selector, only_visible) => {
var elems = document.querySelectorAll(selector); const elems = document.querySelectorAll(selector);
var out = []; const out = [];
for (var i = 0; i < elems.length; ++i) { for (let i = 0; i < elems.length; ++i) {
if (!only_visible || is_visible(elems[i])) { if (!only_visible || is_visible(elems[i])) {
out.push(serialize_elem(elems[i])); out.push(serialize_elem(elems[i]));
} }
@ -172,13 +172,13 @@ window._qutebrowser.webelem = (function() {
return out; return out;
}; };
funcs.find_id = function(id) { funcs.find_id = (id) => {
var elem = document.getElementById(id); const elem = document.getElementById(id);
return serialize_elem(elem); return serialize_elem(elem);
}; };
funcs.find_focused = function() { funcs.find_focused = () => {
var elem = document.activeElement; const elem = document.activeElement;
if (!elem || elem === document.body) { if (!elem || elem === document.body) {
// "When there is no selection, the active element is the page's // "When there is no selection, the active element is the page's
@ -189,43 +189,43 @@ window._qutebrowser.webelem = (function() {
return serialize_elem(elem); return serialize_elem(elem);
}; };
funcs.find_at_pos = function(x, y) { funcs.find_at_pos = (x, y) => {
// FIXME:qtwebengine // FIXME:qtwebengine
// If the element at the specified point belongs to another document // If the element at the specified point belongs to another document
// (for example, an iframe's subdocument), the subdocument's parent // (for example, an iframe's subdocument), the subdocument's parent
// element is returned (the iframe itself). // element is returned (the iframe itself).
var elem = document.elementFromPoint(x, y); const elem = document.elementFromPoint(x, y);
return serialize_elem(elem); return serialize_elem(elem);
}; };
// Function for returning a selection to python (so we can click it) // Function for returning a selection to python (so we can click it)
funcs.find_selected_link = function() { funcs.find_selected_link = () => {
var elem = window.getSelection().anchorNode; const elem = window.getSelection().anchorNode;
if (!elem) { if (!elem) {
return null; return null;
} }
return serialize_elem(elem.parentNode); return serialize_elem(elem.parentNode);
}; };
funcs.set_value = function(id, value) { funcs.set_value = (id, value) => {
elements[id].value = value; elements[id].value = value;
}; };
funcs.insert_text = function(id, text) { funcs.insert_text = (id, text) => {
var elem = elements[id]; const elem = elements[id];
elem.focus(); elem.focus();
document.execCommand("insertText", false, text); document.execCommand("insertText", false, text);
}; };
funcs.set_attribute = function(id, name, value) { funcs.set_attribute = (id, name, value) => {
elements[id].setAttribute(name, value); elements[id].setAttribute(name, value);
}; };
funcs.remove_blank_target = function(id) { funcs.remove_blank_target = (id) => {
var elem = elements[id]; let elem = elements[id];
while (elem !== null) { while (elem !== null) {
var tag = elem.tagName.toLowerCase(); const tag = elem.tagName.toLowerCase();
if (tag === "a" || tag === "area") { if (tag === "a" || tag === "area") {
if (elem.getAttribute("target") === "_blank") { if (elem.getAttribute("target") === "_blank") {
elem.setAttribute("target", "_top"); elem.setAttribute("target", "_top");
@ -236,18 +236,18 @@ window._qutebrowser.webelem = (function() {
} }
}; };
funcs.click = function(id) { funcs.click = (id) => {
var elem = elements[id]; const elem = elements[id];
elem.click(); elem.click();
}; };
funcs.focus = function(id) { funcs.focus = (id) => {
var elem = elements[id]; const elem = elements[id];
elem.focus(); elem.focus();
}; };
funcs.move_cursor_to_end = function(id) { funcs.move_cursor_to_end = (id) => {
var elem = elements[id]; const elem = elements[id];
elem.selectionStart = elem.value.length; elem.selectionStart = elem.value.length;
elem.selectionEnd = elem.value.length; elem.selectionEnd = elem.value.length;
}; };