Skip to content

Commit

Permalink
Add lazy loading function
Browse files Browse the repository at this point in the history
  • Loading branch information
sienori committed Nov 23, 2017
1 parent c19171b commit 501461d
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 18 deletions.
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "Aktuelles Fenster wird überschrieben, falls Haken entfernt wird."
},
"ifLazyLoadingLabel": {
"message": "Tab lazy loading"
},
"ifLazyLoadingCaptionLabel": {
"message": "When opening a session,the contents of each page are not loaded until you select the tab.<br>If checked off, memory usage increases when session is restored."
},
"ifAutoSaveLabel": {
"message": "Sitzung regelmäßig sichern"
},
Expand Down
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "If checked off, the session overwrites the current window."
},
"ifLazyLoadingLabel": {
"message": "Tab lazy loading"
},
"ifLazyLoadingCaptionLabel": {
"message": "When opening a session,the contents of each page are not loaded until you select the tab.<br>If checked off, memory usage increases when session is restored."
},
"ifAutoSaveLabel": {
"message": "Save the session regularly"
},
Expand Down
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "チェックをオフにすると,セッションは現在のウィンドウを上書きします。"
},
"ifLazyLoadingLabel": {
"message": "タブの遅延読み込み"
},
"ifLazyLoadingCaptionLabel": {
"message": "セッションを開いたとき,タブを選択するまで各ページの内容を読み込みません。<br>チェックをオフにすると,セッション復元時のメモリ使用量が増大します。"
},
"ifAutoSaveLabel": {
"message": "定期的にセッションを保存する"
},
Expand Down
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "Если выключено, сессия перезапишет текущее окно."
},
"ifLazyLoadingLabel": {
"message": "Tab lazy loading"
},
"ifLazyLoadingCaptionLabel": {
"message": "When opening a session,the contents of each page are not loaded until you select the tab.<br>If checked off, memory usage increases when session is restored."
},
"ifAutoSaveLabel": {
"message": "Периодически сохранять сессию"
},
Expand Down
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "If checked off, the session overwrites the current window."
},
"ifLazyLoadingLabel": {
"message": "Tab lazy loading"
},
"ifLazyLoadingCaptionLabel": {
"message": "When opening a session,the contents of each page are not loaded until you select the tab.<br>If checked off, memory usage increases when session is restored."
},
"ifAutoSaveLabel": {
"message": "定期保存已打开标签页的状态"
},
Expand Down
6 changes: 6 additions & 0 deletions Tab-Session-Manager/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"ifOpenNewWindowCaptionLabel": {
"message": "如果取消勾選,開啟工作階段時將會覆寫目前視窗"
},
"ifLazyLoadingLabel": {
"message": "Tab lazy loading"
},
"ifLazyLoadingCaptionLabel": {
"message": "When opening a session,the contents of each page are not loaded until you select the tab.<br>If checked off, memory usage increases when session is restored."
},
"ifAutoSaveLabel": {
"message": "定期將開啟的分頁瀏覽狀態自動儲存"
},
Expand Down
81 changes: 73 additions & 8 deletions Tab-Session-Manager/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,54 @@ initSettings().then(function () {
setAutoSaveListener();
autoSaveWhenCloseListener();
browser.storage.onChanged.addListener(loadSessions);
browser.tabs.onActivated.addListener(replacePage);
browser.windows.onFocusChanged.addListener(replacePage);
});

function returnReplaceParamater(url) {
let paramater = {};
if (url.indexOf(browser.runtime.getURL("replaced/replaced.html")) === 0) {
paramater.isReplaced = true;
let paras = url.split('?')[1].split('&');
for (let p of paras) {
paramater[p.split('=')[0]] = decodeURIComponent(p.split('=')[1]);
}
} else {
paramater.isReplaced = false;
}

return paramater;
}

function replacePage() {
if (!IsOpeningSession) {
browser.tabs.query({
active: true,
currentWindow: true
}).then(function (info) {
if (info[0].status != "complete") {
setTimeout(replacePage, 500);
}
let paramater = returnReplaceParamater(info[0].url);
if (paramater.isReplaced && paramater.state == "redirect") {
browser.tabs.update({
url: paramater.url
}).catch(function () {
//失敗時
browser.tabs.update({
url: "replaced/replaced.html" +
"?state=open_faild" +
"&title=" + encodeURIComponent(paramater.title) +
"&url=" + encodeURIComponent(paramater.url) +
"&favIconUrl=" + encodeURIComponent(paramater.favIconUrl)
})
})
}

})
}
}

//設定の初期化
function initSettings(value) {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -92,6 +138,7 @@ function setAutoSaveListener() {
}
}


function autoSaveWhenCloseListener() {
saveSession("Auto Saved - Window was closed", "auto winClose temp").then(function () {
removeOverLimit("winClose");
Expand Down Expand Up @@ -153,6 +200,11 @@ function loadCurrentSesssion(name, tag) {
//windouwsとtabのセット
for (let tab of tabs) {
if (session.windows[tab.windowId] == undefined) session.windows[tab.windowId] = {};
//replacedPageなら元のページを保存
let paramater = returnReplaceParamater(tab.url)
if (paramater.isReplaced) {
tab.url = paramater.url;
}
session.windows[tab.windowId][tab.id] = tab;
session.tabsNumber++;
}
Expand Down Expand Up @@ -255,6 +307,7 @@ function openSession(session) {
}
}

let IsOpeningSession = false;
//ウィンドウとタブを閉じてcurrentWindowを返す
function removeNowOpenTabs() {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -284,6 +337,7 @@ function removeNowOpenTabs() {
//現在のウィンドウにタブを生成
function createTabs(session, win, currentWindow) {
return new Promise(function (resolve, reject) {
IsOpeningSession = true;
let sortedTabs = [];

for (let tab in session.windows[win]) {
Expand All @@ -302,13 +356,10 @@ function createTabs(session, win, currentWindow) {
}).then(function () {
if (tabNumber == 1) {
browser.tabs.remove(firstTabId);
}
if (tabNumber == Object.keys(session.windows[win]).length) {
} else if (tabNumber == Object.keys(session.windows[win]).length) {
IsOpeningSession = false;
replacePage();
resolve();
/*sortTabsを導入したため不要
moveTabsInIndex(currentWindow).then(function () {
resolve();
});*/
}
});
}
Expand Down Expand Up @@ -338,16 +389,29 @@ function openTab(session, win, currentWindow, tab) {
openDelay = 0;
}

//Lazy loading
if (S.get().ifLazyLoading) {
createOption.url = "replaced/replaced.html" +
"?state=redirect" +
"&title=" + encodeURIComponent(property.title) +
"&url=" + encodeURIComponent(property.url) +
"&favIconUrl=" + encodeURIComponent(property.favIconUrl);
}

setTimeout(function () {
browser.tabs.create(createOption).then(function (newTab) {
tabList[property.id] = newTab.id;
resolve();
}, function () { //タブオープン失敗時
createOption.url = "replaced/replaced.html" + "?tst_title=" + property.title + "&tst_url=" + property.url;
createOption.url = "replaced/replaced.html" +
"?state=open_faild" +
"&title=" + encodeURIComponent(property.title) +
"&url=" + encodeURIComponent(property.url) +
"&favIconUrl=" + encodeURIComponent(property.favIconUrl)
browser.tabs.create(createOption).then(function (newTab) {
tabList[property.id] = newTab.id;
resolve();
}, function () {
}, function () { //失敗時(多分起こらないけど念のため)
resolve();
})
});
Expand Down Expand Up @@ -379,6 +443,7 @@ function removeSession(number) {
}



//popupからのリクエスト
browser.runtime.onMessage.addListener(function (request) {
switch (request.message) {
Expand Down
17 changes: 17 additions & 0 deletions Tab-Session-Manager/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@
</div>
</div>
</li>

<hr>

<li>
<div class=optionContainer>
<div class=optionText>
<p class=ifLazyLoadingLabel>タブを遅延読み込み</p>
<p class="caption ifLazyLoadingCaptionLabel">推奨 セッションを開いたとき,タブを選択するまでページの内容を読み込みません</p>
</div>
<div class="optionForm">
<label>
<input type="checkbox" id="ifLazyLoading" class=saveByChange checked=true>
<span class=checkbox></span>
</label>
</div>
</div>
</li>
<hr>

<li>
Expand Down
6 changes: 3 additions & 3 deletions Tab-Session-Manager/replaced/replaced.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ body {
padding-left: 20px;
}

.replacedUrl {
font-size: 33px;
font-weight: 200;
.title {
font-size: 22px;
font-weight: 600;
color: var(--sub-text);
line-height: 2;
}
Expand Down
5 changes: 3 additions & 2 deletions Tab-Session-Manager/replaced/replaced.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

<body>
<div class=container>
<div class=replacedUrl>about:example</div>
<div id=replacedPageMessage class=caption>このページはTab Session Managerでは開くことが出来ません。</div>
<div class=title>Title</div>
<div class="replacedUrl caption">about:example</div>
<div class="replacedPageMessage caption"></div>
</div>
<script type="text/javascript" src="replaced.js"></script>
</body>
Expand Down
23 changes: 18 additions & 5 deletions Tab-Session-Manager/replaced/replaced.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

let title = location.href.split('?tst_title=').slice(1).join('?').split('&tst_url')[0];
let url = location.href.split('&tst_url=').slice(1).join('?');
let paramater = returnReplaceParamater(location.href);

document.title = decodeURI(title);
document.getElementsByClassName("replacedUrl")[0].innerText = decodeURI(url);
document.getElementById("replacedPageMessage").innerText = browser.i18n.getMessage("replacedPageMessage");
document.title = paramater.title;
document.getElementsByClassName("title")[0].innerText = paramater.title;
document.getElementsByClassName("replacedUrl")[0].innerText = paramater.url;
document.head.insertAdjacentHTML('beforeend', '<link rel = "shortcut icon" href =' + paramater.favIconUrl + '>');

if (paramater.state == "open_faild") {
document.getElementsByClassName("replacedPageMessage")[0].innerText = browser.i18n.getMessage("replacedPageMessage");
}

function returnReplaceParamater(url) {
let paramater = {};
let paras = url.split('?')[1].split('&');
for (let p of paras) {
paramater[p.split('=')[0]] = decodeURIComponent(p.split('=')[1]);
}
return paramater;
}

0 comments on commit 501461d

Please sign in to comment.