File indexing completed on 2024-12-22 04:41:05

0001 // ==UserScript==
0002 // @name     _falkon_restore
0003 // @run-at   document-end
0004 // @include  falkon:restore
0005 // ==/UserScript==
0006 
0007 (function() {
0008 
0009 var scriptData = {};
0010 var selectedRow = null;
0011 
0012 function selectRow(row)
0013 {
0014     if (selectedRow) {
0015         selectedRow.className = selectedRow.className.replace(/\bselected\b/, "");
0016     }
0017 
0018     row.className = row.className + " selected";
0019     selectedRow = row;
0020 }
0021 
0022 function forEachInput(f)
0023 {
0024     var inputs = document.getElementsByTagName("input");
0025     for (var i = 0; i < inputs.length; ++i) {
0026         f(inputs[i]);
0027     }
0028 }
0029 
0030 function toggleWindow(e)
0031 {
0032     var win = e.getAttribute("data-window");
0033 
0034     forEachInput(function(input) {
0035         if (input.getAttribute("data-window") == win) {
0036             input.checked = e.checked;
0037         }
0038     });
0039 }
0040 
0041 function toggleTab(e)
0042 {
0043     var win = e.getAttribute("data-window");
0044     var winElement = null;
0045     var checked = 0;
0046     var total = 0;
0047 
0048     forEachInput(function(input) {
0049         if (input.getAttribute("data-window") != win) {
0050             return;
0051         }
0052         if (!input.hasAttribute("data-tab")) {
0053             winElement = input;
0054             return;
0055         }
0056         if (input.checked) {
0057             ++checked;
0058         }
0059         ++total;
0060     });
0061 
0062     if (checked == total) {
0063         winElement.checked = true;
0064         winElement.indeterminate = false;
0065     } else if (checked > 0) {
0066         winElement.indeterminate = true;
0067     } else {
0068         winElement.checked = false;
0069         winElement.indeterminate = false;
0070     }
0071 }
0072 
0073 function startNewSession()
0074 {
0075     document.getElementById("start-new-session-button").disabled = true;
0076     external.recovery.startNewSession();
0077 }
0078 
0079 function restoreSession()
0080 {
0081     document.getElementById("restore-session-button").disabled = true;
0082 
0083     var excludeWin = [];
0084     var excludeTab = [];
0085 
0086     forEachInput(function(input) {
0087         if (input.checked || input.indeterminate || !input.hasAttribute("data-tab")) {
0088             return;
0089         }
0090         excludeWin.unshift(input.getAttribute("data-window"));
0091         excludeTab.unshift(input.getAttribute("data-tab"));
0092     });
0093 
0094     external.recovery.restoreSession(excludeWin, excludeTab);
0095 }
0096 
0097 function addWindow(winId)
0098 {
0099     var tr = document.createElement("tr");
0100     tr.className = "window";
0101     tr.onclick = function() { selectRow(tr); };
0102     var td = document.createElement("td");
0103     var input = document.createElement("input");
0104     input.type = "checkbox";
0105     input.checked = true;
0106     input.setAttribute("data-window", winId);
0107     input.onclick = function() { toggleWindow(input); };
0108     var span = document.createElement("span");
0109     span.innerText = scriptData.window + " " + (winId + 1);
0110 
0111     tr.appendChild(td);
0112     td.appendChild(input);
0113     td.appendChild(span);
0114 
0115     document.getElementById("recovery-items").appendChild(tr);
0116 }
0117 
0118 function addTab(winId, tab)
0119 {
0120     var tr = document.createElement("tr");
0121     tr.className = "tab";
0122     tr.title = tab.url;
0123     tr.onclick = function() { selectRow(tr); };
0124     var td = document.createElement("td");
0125     var input = document.createElement("input");
0126     input.type = "checkbox";
0127     input.checked = true;
0128     input.setAttribute("data-window", winId);
0129     input.setAttribute("data-tab", tab.tab);
0130     input.onclick = function() { toggleTab(input); };
0131     var img = document.createElement("img");
0132     img.src = tab.icon;
0133     var span = document.createElement("span");
0134     span.innerText = tab.title;
0135 
0136     if (tab.pinned) {
0137         span.innerText = "🖈 " + span.innerText;
0138     }
0139     if (tab.current) {
0140         span.style.fontStyle = 'italic';
0141     }
0142 
0143     tr.appendChild(td);
0144     td.appendChild(input);
0145     td.appendChild(img);
0146     td.appendChild(span);
0147 
0148     document.getElementById("recovery-items").appendChild(tr);
0149 }
0150 
0151 function init()
0152 {
0153     scriptData = document.getElementById("script-data").dataset;
0154 
0155     document.getElementById("start-new-session-button").onclick = function() {
0156         startNewSession();
0157         return false;
0158     };
0159 
0160     document.getElementById("restore-session-button").onclick = function() {
0161         restoreSession();
0162         return false;
0163     };
0164 
0165     var data = external.recovery.restoreData;
0166     for (var i = 0; i < data.length; ++i) {
0167         var win = data[i];
0168         addWindow(win.window);
0169         for (var j = 0; j < win.tabs.length; ++j) {
0170             var tab = win.tabs[j];
0171             addTab(win.window, tab);
0172         }
0173     }
0174 }
0175 
0176 // Initialize
0177 if (window._falkon_external) {
0178     init();
0179 } else {
0180     document.addEventListener("_falkon_external_created", init);
0181 }
0182 
0183 })();