Skip to content

Commit

Permalink
0.9.53
Browse files Browse the repository at this point in the history
Performance improvement: create components for frames only when it is necessary.
  • Loading branch information
brookhong committed Nov 7, 2019
1 parent 13a5d8a commit 982942c
Show file tree
Hide file tree
Showing 27 changed files with 336 additions and 334 deletions.
2 changes: 1 addition & 1 deletion background.js
Expand Up @@ -1112,7 +1112,7 @@ var ChromeService = (function() {
var tid = sender.tab.id;
chrome.tabs.executeScript(tid, {
allFrames: true,
code: "Front && Front.getFrameId && Front.getFrameId()"
code: "typeof(getFrameId) === 'function' && getFrameId()"
}, function(framesInTab) {
framesInTab = framesInTab.filter(function(frameId) {
return frameId;
Expand Down
4 changes: 2 additions & 2 deletions content_scripts/clipboard.js
@@ -1,4 +1,4 @@
var Clipboard = (function(mode) {
function createClipboard() {
var self = {};

var holder = document.createElement('textarea');
Expand Down Expand Up @@ -43,4 +43,4 @@ var Clipboard = (function(mode) {

return self;

})();
}
5 changes: 2 additions & 3 deletions content_scripts/content_scripts.css
Expand Up @@ -72,9 +72,8 @@ div.surfingkeys_cursor:empty {
}
#sk_frame {
position: fixed;
border: 4px solid #3CF30F;
border: 4px solid #434343;
box-sizing: border-box;
z-index: 2147483000;
background: rgba(129, 120, 222, 0.62);
box-shadow: 0px 0px 10px rgba(218, 60, 13, 0.8);
background: rgba(202, 202, 202, 0.62);
}
157 changes: 149 additions & 8 deletions content_scripts/content_scripts.js
Expand Up @@ -323,7 +323,7 @@ function applySettings(rs) {
if ('findHistory' in rs) {
runtime.conf.lastQuery = rs.findHistory.length ? rs.findHistory[0] : "";
}
if ('snippets' in rs && rs.snippets && !Front.isProvider()) {
if ('snippets' in rs && rs.snippets && !isInUIFrame()) {
var delta = runScript(rs.snippets);
if (delta.error !== "") {
if (window === top) {
Expand Down Expand Up @@ -360,7 +360,65 @@ function applySettings(rs) {
}
}

runtime.on('settingsUpdated', function(response) {
function _initModules() {
window.KeyboardUtils = createKeyboardUtils();
window.Mode = createMode();
window.Normal = createNormal();
Normal.enter();
window.Disabled = createDisabled();
window.PassThrough = createPassThrough();
window.Insert = createInsert();
window.Visual = createVisual();
window.Hints = createHints();
window.Clipboard = createClipboard();
window.Front = createFront();
createDefaultMappings();
}

function _initObserver() {
var pendingUpdater = undefined;
new MutationObserver(function (mutations) {
var addedNodes = [];
for (var m of mutations) {
for (var n of m.addedNodes) {
if (n.nodeType === Node.ELEMENT_NODE && !n.fromSurfingKeys) {
n.newlyCreated = true;
addedNodes.push(n);
}
}
}

if (addedNodes.length) {
if (pendingUpdater) {
clearTimeout(pendingUpdater);
pendingUpdater = undefined;
}
pendingUpdater = setTimeout(function() {
var possibleModalElements = getVisibleElements(function(e, v) {
var br = e.getBoundingClientRect();
if (br.width > 300 && br.height > 300
&& br.width <= window.innerWidth && br.height <= window.innerHeight
&& br.top >= 0 && br.left >= 0
) {
var originalTop = document.scrollingElement.scrollTop;
document.scrollingElement.scrollTop += 1;
var br1 = e.getBoundingClientRect();
if (br.top === br1.top && hasScroll(e, 'y', 16)) {
v.push(e);
}
document.scrollingElement.scrollTop = originalTop;
}
});

if (possibleModalElements.length) {
Normal.addScrollableElement(possibleModalElements[0]);
}
}, 200);
}
}).observe(document.body, { childList: true, subtree:true });;
}

function _onSettingsUpdated(response) {
var rs = response.settings;
applySettings(rs);
if (rs.hasOwnProperty('blacklist') || runtime.conf.blacklistPattern) {
Expand All @@ -384,17 +442,23 @@ runtime.on('settingsUpdated', function(response) {
}
});
}
});
}

function _init() {
function _initContent() {
_initObserver();
window.frameId = generateQuickGuid();
runtime.on('settingsUpdated', _onSettingsUpdated);
runtime.command({
action: 'getSettings'
}, function(response) {
var rs = response.settings;

applySettings(rs);

Normal.enter();
if (runtime.conf.stealFocusOnLoad && !isInUIFrame()) {
var elm = getRealEdit();
elm && elm.blur();
}

runtime.command({
action: 'getDisabled',
Expand Down Expand Up @@ -427,6 +491,83 @@ function _init() {
});
}

document.addEventListener("surfingkeys:defaultSettingsLoaded", function (evt) {
_init();
});
function getFrameId() {
if (!window.frameId && window.innerWidth > 16 && window.innerHeight > 16
&& runtime.conf.ignoredFrameHosts.indexOf(window.origin) === -1
&& (!window.frameElement || parseInt("0" + getComputedStyle(window.frameElement).zIndex) >= 0)
) {
_initModules();
_initContent();
}
return window.frameId;
}

if (window === top) {
_initModules();

document.addEventListener('DOMContentLoaded', function (e) {
_initContent();
if (document.contentType === "application/pdf") {
// Appending child to document will break default pdf viewer from rendering.
// So we append child after default pdf viewer rendered.
document.body.querySelector("EMBED").addEventListener("load", function(evt) {
setTimeout(function() {
document.documentElement.appendChild(createUiHost());
}, 10);
});
} else {
document.documentElement.appendChild(createUiHost());
}
window._setScrollPos = function (x, y) {
document.scrollingElement.scrollLeft = x;
document.scrollingElement.scrollTop = y;
};

runtime.command({
action: 'tabURLAccessed',
title: document.title,
url: window.location.href
}, function (resp) {
if (resp.index > 0) {
var showTabIndexInTitle = function () {
skipObserver = true;
document.title = myTabIndex + " " + originalTitle;
};

var myTabIndex = resp.index,
skipObserver = false,
originalTitle = document.title;

new MutationObserver(function (mutationsList) {
if (skipObserver) {
skipObserver = false;
} else {
originalTitle = document.title;
showTabIndexInTitle();
}
}).observe(document.querySelector("title"), { childList: true });;

showTabIndexInTitle();

runtime.runtime_handlers['tabIndexChange'] = function(msg, sender, response) {
if (msg.index !== myTabIndex) {
myTabIndex = msg.index;
showTabIndexInTitle();
}
};
}
});

setTimeout(function () {
// to avoid conflict with pdf extension: chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/
for (var p in AutoCommands) {
var c = AutoCommands[p];
if (c.regex.test(window.location.href)) {
c.code();
}
}
}, 0);

// There is some site firing DOMContentLoaded twice, such as http://www.423down.com/
}, {once: true});
}
82 changes: 8 additions & 74 deletions content_scripts/front.js
@@ -1,14 +1,9 @@
var Front = (function() {
function createFront() {
var self = {};
// The agent is a front stub to talk with pages/frontend.html
// that will live in all content window except the frontend.html
// as there is no need to make this object live in frontend.html.

// this object is stub of UI, it's UI consumer
self.isProvider = function() {
return document.location.href.indexOf(chrome.extension.getURL("")) === 0;
};

var frontendPromise = new Promise(function (resolve, reject) {
if (window === top) {
self.resolve = resolve;
Expand Down Expand Up @@ -252,7 +247,7 @@ var Front = (function() {
}, function(res) {
showQueryResult(_inlineQuery.parseResult(res));
});
} else if (Front.isProvider()) {
} else if (isInUIFrame()) {
_showQueryResult = showQueryResult;
document.getElementById("proxyFrame").contentWindow.postMessage({
action: "performInlineQuery",
Expand Down Expand Up @@ -361,16 +356,6 @@ var Front = (function() {
});
};

self.getFrameId = function () {
if (document.body.offsetWidth > 16 && document.body.offsetHeight > 16 && document.body.innerText
&& (!window.frameElement || getComputedStyle(window.frameElement).zIndex >= 0)
&& runtime.conf.ignoredFrameHosts.indexOf(window.origin) === -1
&& !window.frameId) {
window.frameId = generateQuickGuid();
}
return window.frameId;
};

_actions["ace_editor_saved"] = function(response) {
if (response.data !== undefined) {
onEditorSaved(response.data);
Expand Down Expand Up @@ -429,7 +414,7 @@ var Front = (function() {

_actions["getBackFocus"] = function(response) {
window.focus();
if (window === top && document.activeElement === ifr[0]) {
if (window === top && document.activeElement === window.uiFrame) {
// fix for Firefox, blur from iframe for frontend after Omnibar closed.
document.activeElement.blur();
}
Expand Down Expand Up @@ -490,69 +475,18 @@ var Front = (function() {
block: 'center',
inline: 'center'
});
var rc = document.body.getBoundingClientRect();
self.highlightElement({
duration: 500,
rect: {
top: rc.top,
left: rc.left,
width: rc.width,
height: rc.height
top: 0,
left: 0,
width: window.innerWidth,
height: window.innerHeight
}
});

Normal.exit();
Normal.enter();
}
};

document.addEventListener('DOMContentLoaded', function (e) {
if (window.location.href.indexOf(chrome.extension.getURL("/pages/pdf_viewer.html")) === 0) {
document.getElementById("proxyFrame").src = window.location.search.substr(3);
}

var pendingUpdater = undefined;
new MutationObserver(function (mutations) {
var addedNodes = [];
for (var m of mutations) {
for (var n of m.addedNodes) {
if (n.nodeType === Node.ELEMENT_NODE && !n.fromSurfingKeys) {
n.newlyCreated = true;
addedNodes.push(n);
}
}
}

if (addedNodes.length) {
if (pendingUpdater) {
clearTimeout(pendingUpdater);
pendingUpdater = undefined;
}
pendingUpdater = setTimeout(function() {
var possibleModalElements = getVisibleElements(function(e, v) {
var br = e.getBoundingClientRect();
if (br.width > 300 && br.height > 300
&& br.width <= window.innerWidth && br.height <= window.innerHeight
&& br.top >= 0 && br.left >= 0
) {
var originalTop = document.scrollingElement.scrollTop;
document.scrollingElement.scrollTop += 1;
var br1 = e.getBoundingClientRect();
if (br.top === br1.top && hasScroll(e, 'y', 16)) {
v.push(e);
}
document.scrollingElement.scrollTop = originalTop;
}
});

if (possibleModalElements.length) {
Normal.addScrollableElement(possibleModalElements[0]);
}
}, 200);
}
}).observe(document.body, { childList: true, subtree:true });;
});

window.addEventListener('message', function (event) {
var _message = event.data;
if (_message.action === "performInlineQuery") {
Expand Down Expand Up @@ -588,4 +522,4 @@ var Front = (function() {
}, true);

return self;
})();
}
4 changes: 2 additions & 2 deletions content_scripts/hints.js
@@ -1,4 +1,4 @@
var Hints = (function() {
function createHints() {
var self = new Mode("Hints");

self.addEventListener('keydown', function(event) {
Expand Down Expand Up @@ -630,4 +630,4 @@ var Hints = (function() {
};

return self;
})();
}
4 changes: 2 additions & 2 deletions content_scripts/insert.js
@@ -1,4 +1,4 @@
var Insert = (function() {
function createInsert() {
var self = new Mode("Insert");

function moveCusorEOL() {
Expand Down Expand Up @@ -420,4 +420,4 @@ var Insert = (function() {
};

return self;
})();
}
4 changes: 2 additions & 2 deletions content_scripts/keyboardUtils.js
@@ -1,4 +1,4 @@
var KeyboardUtils = (function() {
function createKeyboardUtils() {
var self = {
keyCodesMac: {
Minus: ["-", "_"],
Expand Down Expand Up @@ -226,7 +226,7 @@ var KeyboardUtils = (function() {
};

return self;
})();
}


/*
Expand Down

0 comments on commit 982942c

Please sign in to comment.