Falkon Develop
Cross-platform Qt-based web browser
restore.user.js
Go to the documentation of this file.
1// ==UserScript==
2// @name _falkon_restore
3// @run-at document-end
4// @include falkon:restore
5// ==/UserScript==
6
7(function() {
8
9var scriptData = {};
10var selectedRow = null;
11
12function selectRow(row)
13{
14 if (selectedRow) {
15 selectedRow.className = selectedRow.className.replace(/\bselected\b/, "");
16 }
17
18 row.className = row.className + " selected";
19 selectedRow = row;
20}
21
22function forEachInput(f)
23{
24 var inputs = document.getElementsByTagName("input");
25 for (var i = 0; i < inputs.length; ++i) {
26 f(inputs[i]);
27 }
28}
29
30function toggleWindow(e)
31{
32 var win = e.getAttribute("data-window");
33
34 forEachInput(function(input) {
35 if (input.getAttribute("data-window") == win) {
36 input.checked = e.checked;
37 }
38 });
39}
40
41function toggleTab(e)
42{
43 var win = e.getAttribute("data-window");
44 var winElement = null;
45 var checked = 0;
46 var total = 0;
47
48 forEachInput(function(input) {
49 if (input.getAttribute("data-window") != win) {
50 return;
51 }
52 if (!input.hasAttribute("data-tab")) {
53 winElement = input;
54 return;
55 }
56 if (input.checked) {
57 ++checked;
58 }
59 ++total;
60 });
61
62 if (checked == total) {
63 winElement.checked = true;
64 winElement.indeterminate = false;
65 } else if (checked > 0) {
66 winElement.indeterminate = true;
67 } else {
68 winElement.checked = false;
69 winElement.indeterminate = false;
70 }
71}
72
73function startNewSession()
74{
75 document.getElementById("start-new-session-button").disabled = true;
76 external.recovery.startNewSession();
77}
78
79function restoreSession()
80{
81 document.getElementById("restore-session-button").disabled = true;
82
83 var excludeWin = [];
84 var excludeTab = [];
85
86 forEachInput(function(input) {
87 if (input.checked || input.indeterminate || !input.hasAttribute("data-tab")) {
88 return;
89 }
90 excludeWin.unshift(input.getAttribute("data-window"));
91 excludeTab.unshift(input.getAttribute("data-tab"));
92 });
93
94 external.recovery.restoreSession(excludeWin, excludeTab);
95}
96
97function addWindow(winId)
98{
99 var tr = document.createElement("tr");
100 tr.className = "window";
101 tr.onclick = function() { selectRow(tr); };
102 var td = document.createElement("td");
103 var input = document.createElement("input");
104 input.type = "checkbox";
105 input.checked = true;
106 input.setAttribute("data-window", winId);
107 input.onclick = function() { toggleWindow(input); };
108 var span = document.createElement("span");
109 span.innerText = scriptData.window + " " + (winId + 1);
110
111 tr.appendChild(td);
112 td.appendChild(input);
113 td.appendChild(span);
114
115 document.getElementById("recovery-items").appendChild(tr);
116}
117
118function addTab(winId, tab)
119{
120 var tr = document.createElement("tr");
121 tr.className = "tab";
122 tr.title = tab.url;
123 tr.onclick = function() { selectRow(tr); };
124 var td = document.createElement("td");
125 var input = document.createElement("input");
126 input.type = "checkbox";
127 input.checked = true;
128 input.setAttribute("data-window", winId);
129 input.setAttribute("data-tab", tab.tab);
130 input.onclick = function() { toggleTab(input); };
131 var img = document.createElement("img");
132 img.src = tab.icon;
133 var span = document.createElement("span");
134 span.innerText = tab.title;
135
136 if (tab.pinned) {
137 span.innerText = "🖈 " + span.innerText;
138 }
139 if (tab.current) {
140 span.style.fontStyle = 'italic';
141 }
142
143 tr.appendChild(td);
144 td.appendChild(input);
145 td.appendChild(img);
146 td.appendChild(span);
147
148 document.getElementById("recovery-items").appendChild(tr);
149}
150
151function init()
152{
153 scriptData = document.getElementById("script-data").dataset;
154
155 document.getElementById("start-new-session-button").onclick = function() {
156 startNewSession();
157 return false;
158 };
159
160 document.getElementById("restore-session-button").onclick = function() {
161 restoreSession();
162 return false;
163 };
164
165 var data = external.recovery.restoreData;
166 for (var i = 0; i < data.length; ++i) {
167 var win = data[i];
168 addWindow(win.window);
169 for (var j = 0; j < win.tabs.length; ++j) {
170 var tab = win.tabs[j];
171 addTab(win.window, tab);
172 }
173 }
174}
175
176// Initialize
177if (window._falkon_external) {
178 init();
179} else {
180 document.addEventListener("_falkon_external_created", init);
181}
182
183})();