Skip to content

Commit

Permalink
0.9.54
Browse files Browse the repository at this point in the history
create ui frame only for active tab to reduce memory usage
  • Loading branch information
brookhong committed Nov 17, 2019
1 parent 775f6ee commit 664d042
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 71 deletions.
17 changes: 17 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,28 @@ var ChromeService = (function() {
delete tabMessages[tabId];
}
}
var _lastActiveTabId = null;
function _tabActivated(tabId) {
if (_lastActiveTabId !== null) {
chrome.tabs.sendMessage(_lastActiveTabId, {
subject: 'tabDeactivated'
});
}
chrome.tabs.sendMessage(tabId, {
subject: 'tabActivated'
});
_lastActiveTabId = tabId;
}
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === "loading") {
if (changeInfo.url !== undefined && changeInfo.url !== chrome.extension.getURL("pages/error.html")) {
delete tabErrors[tabId];
}
delete frameIndexes[tabId];
} else if (changeInfo.status === "complete") {
if (tab.active) {
_tabActivated(tabId);
}
}
_setScrollPos_bg(tabId);
});
Expand All @@ -347,6 +363,7 @@ var ChromeService = (function() {
tabHistoryIndex = tabHistory.length - 1;
}
tabActivated[activeInfo.tabId] = new Date().getTime();
_tabActivated(activeInfo.tabId);
historyTabAction = false;
chromelikeNewTabPosition = 0;

Expand Down
26 changes: 14 additions & 12 deletions content_scripts/content_scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function applySettings(rs) {
}
}
if (!isEmptyObject(delta.settings)) {
Front.applyUserSettings(JSON.parse(JSON.stringify(delta.settings)));
Front.setUserSettings(JSON.parse(JSON.stringify(delta.settings)));
// overrides local settings from snippets
for (var k in delta.settings) {
if (runtime.conf.hasOwnProperty(k)) {
Expand Down Expand Up @@ -515,17 +515,19 @@ if (window === top) {

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());
}
runtime.on('tabActivated', function() {
if (!window.uiHost) {
window.uiHost = createUiHost();
document.documentElement.appendChild(window.uiHost);
}
});
runtime.on('tabDeactivated', function() {
if (window.uiHost) {
window.uiHost.detach();
window.uiHost.remove();
delete window.uiHost;
}
});
window._setScrollPos = function (x, y) {
document.scrollingElement.scrollLeft = x;
document.scrollingElement.scrollTop = y;
Expand Down
22 changes: 14 additions & 8 deletions content_scripts/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ function createFront() {
});
}

self.applyUserSettings = function (us) {
frontendCommand({
var _uiUserSettings = [];
self.setUserSettings = function (us) {
_uiUserSettings.push({
action: 'applyUserSettings',
userSettings: us
});
};
self.applyUserSettings = function () {
for (var cmd of _uiUserSettings) {
frontendCommand(cmd);
}
};

var _listSuggestions = {};
self.addSearchAlias = function (alias, prompt, url, suggestionURL, listSuggestion) {
if (suggestionURL && listSuggestion) {
_listSuggestions[suggestionURL] = listSuggestion;
}
frontendCommand({
_uiUserSettings.push({
action: 'addSearchAlias',
alias: alias,
prompt: prompt,
Expand All @@ -47,7 +53,7 @@ function createFront() {
});
};
self.removeSearchAlias = function (alias) {
frontendCommand({
_uiUserSettings.push({
action: 'removeSearchAlias',
alias: alias
});
Expand Down Expand Up @@ -99,23 +105,23 @@ function createFront() {
});
};
self.addMapkey = function (mode, new_keystroke, old_keystroke) {
frontendCommand({
_uiUserSettings.push({
action: 'addMapkey',
mode: mode,
new_keystroke: new_keystroke,
old_keystroke: old_keystroke
});
};
self.addVimMap = function (lhs, rhs, ctx) {
frontendCommand({
_uiUserSettings.push({
action: 'addVimMap',
lhs: lhs,
rhs: rhs,
ctx: ctx
});
};
self.addVimKeyMap = function (vimKeyMap) {
frontendCommand({
_uiUserSettings.push({
action: 'addVimKeyMap',
vimKeyMap: vimKeyMap
});
Expand Down Expand Up @@ -413,7 +419,7 @@ function createFront() {

_actions["getBackFocus"] = function(response) {
window.focus();
if (window === top && document.activeElement === window.uiFrame) {
if (window === top && window.uiHost && window.uiHost.shadowRoot.contains(document.activeElement)) {
// fix for Firefox, blur from iframe for frontend after Omnibar closed.
document.activeElement.blur();
}
Expand Down
107 changes: 57 additions & 50 deletions content_scripts/uiframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,72 @@ function createUiHost() {
uiHost.shadowRoot.appendChild(sk_style);
uiHost.shadowRoot.appendChild(ifr);

ifr.addEventListener("load", function() {
this.contentWindow.postMessage({
action: 'initFrontend',
ack: true,
origin: getDocumentOrigin()
}, frontEndURL);

window.addEventListener('message', function(event) {
var _message = event.data;
if (_message === undefined) {
return;
}
if (_message.commandToFrontend || _message.responseToFrontend) {
// forward message to frontend
ifr.contentWindow.postMessage(_message, frontEndURL);
if (_message.commandToFrontend && event.source && _message.action === 'showStatus') {
if (!activeContent || activeContent.window !== event.source) {
// reset active Content

if (activeContent) {
activeContent.window.postMessage({
action: 'deactivated',
direct: true,
reason: `${_message.action}@${event.timeStamp}`,
commandToContent: true
}, activeContent.origin);
}

activeContent = {
window: event.source,
origin: _message.origin
};
function _onWindowMessage(event) {
var _message = event.data;
if (_message === undefined) {
return;
}
if (_message.commandToFrontend || _message.responseToFrontend) {
// forward message to frontend
ifr.contentWindow.postMessage(_message, frontEndURL);
if (_message.commandToFrontend && event.source && _message.action === 'showStatus') {
if (!activeContent || activeContent.window !== event.source) {
// reset active Content

if (activeContent) {
activeContent.window.postMessage({
action: 'activated',
action: 'deactivated',
direct: true,
reason: `${_message.action}@${event.timeStamp}`,
commandToContent: true
}, activeContent.origin);
}

activeContent = {
window: event.source,
origin: _message.origin
};

activeContent.window.postMessage({
action: 'activated',
direct: true,
reason: `${_message.action}@${event.timeStamp}`,
commandToContent: true
}, activeContent.origin);
}
if (_message.action === "visualUpdatedForFirefox") {
document.activeElement.blur();
}
} else if (_message.action && _actions.hasOwnProperty(_message.action)) {
_actions[_message.action](_message);
} else if (_message.commandToContent || _message.responseToContent) {
// forward message to content
if (activeContent && !_message.direct && activeContent.window !== top) {
activeContent.window.postMessage(_message, activeContent.origin);
}
}
}, true);
if (_message.action === "visualUpdatedForFirefox") {
document.activeElement.blur();
}
} else if (_message.action && _actions.hasOwnProperty(_message.action)) {
_actions[_message.action](_message);
} else if (_message.commandToContent || _message.responseToContent) {
// forward message to content
if (activeContent && !_message.direct && activeContent.window !== top) {
activeContent.window.postMessage(_message, activeContent.origin);
}
}
}

ifr.addEventListener("load", function() {
this.contentWindow.postMessage({
action: 'initFrontend',
ack: true,
origin: getDocumentOrigin()
}, frontEndURL);

window.addEventListener('message', _onWindowMessage, true);

}, false);

var lastStateOfPointerEvents = "none", _origOverflowY;
var _actions = {}, activeContent = null, _initialized = false;
var _actions = {}, activeContent = null;
_actions['initFrontendAck'] = function(response) {
if (!_initialized) {
_initialized = true;
if (Front.resolve) {
Front.resolve(window.location.href);
Front.resolve = null;
}
Front.applyUserSettings();
};
_actions['setFrontFrame'] = function(response) {
ifr.style.height = response.frameHeight;
Expand Down Expand Up @@ -106,9 +108,14 @@ function createUiHost() {
lastStateOfPointerEvents = response.pointerEvents;
};

window.uiFrame = ifr;
window.addEventListener('beforeunload', function () {
function _onBeforeunload() {
uiHost.remove();
});
}
window.addEventListener('beforeunload', _onBeforeunload);

uiHost.detach = function() {
window.removeEventListener('message', _onWindowMessage, true);
window.removeEventListener('beforeunload', _onBeforeunload);
};
return uiHost;
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Surfingkeys",
"short_name": "Rich shortcuts in vim spirit for productivity with keyboard.",
"version": "0.9.53",
"version": "0.9.54",
"description": "Rich shortcuts to click links/switch tabs/scroll pages or capture full page, use Chrome like vim for productivity.",
"icons": {
"16": "icons/16.png",
Expand Down
30 changes: 30 additions & 0 deletions pages/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
# Changelog of Surfingkeys

## 0.9.54
* create ui frame only for active tab to reduce memory usage
* remove long-live ports for message passing
* apply user settings as soon as possible
* Fixed #1074 Can't search in help
* add isElementPositionRelative to detect modal elements
* Fix issues on a page with frameset as its body
* Fixed #1071 unexpected removal of scrollNode
* Fixed #1072 disruptive issue of initScrollIndex
* add label for flash frame
* Fixed #1068 issue of autocmd
* Fixed #1069 mitigate issue of unexpected scroll event
* create components for frame when user clicks on it

## 0.9.53
* Performance improvement: create components for frames only when it is necessary.

## 0.9.52
* Fix issue of detecting modal element.
* Remove settings.passThroughTimeout, add PassThrough.setTimeout, so that we could have both passThrough mode and ephemeral passThrough mode simultaneously.

## 0.9.51
* suppress next scroll event on following cases:
- call to check hasScroll
- when scroll to detect modal element, firing scroll event is not expected

## 0.9.50
* Fixed #1055 issue of detecting modal elements

## 0.9.49
* Skip some frames invisible to users.
* Fixed issues of paste data from copied form.
Expand Down

0 comments on commit 664d042

Please sign in to comment.