update readme
[dotfiles/.git] / .config / BraveSoftware / Brave-Browser / Default / Extensions / cimiefiiaegbelhefglklhhakcgmhkai / 1.7.6_0 / extension-tabsrunner.js
1 /*
2     Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de>
3
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License as
6     published by the Free Software Foundation; either version 3 of
7     the License, or (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 addCallback("tabsrunner", "activate", function (message) {
19     var tabId = message.tabId;
20
21     console.log("Tabs Runner requested to activate tab with id", tabId);
22
23     raiseTab(tabId);
24 });
25
26 addCallback("tabsrunner", "setMuted", function (message) {
27
28     var tabId = message.tabId;
29     var muted = message.muted;
30
31     chrome.tabs.update(tabId, {muted: muted}, function (tab) {
32
33         if (chrome.runtime.lastError || !tab) { // this "lastError" stuff feels so archaic
34             // failed to mute/unmute
35             return;
36         }
37     });
38
39 });
40
41 // only forward certain tab properties back to our host
42 var whitelistedTabProperties = [
43     "id", "active", "audible", "favIconUrl", "incognito", "title", "url", "mutedInfo"
44 ];
45
46 // FIXME We really should enforce some kind of security policy, so only e.g. plasmashell and krunner
47 // may access your tabs
48 addCallback("tabsrunner", "getTabs", function (message) {
49     chrome.tabs.query({}, function (tabs) {
50         // remove incognito tabs and properties not in whitelist
51         var filteredTabs = tabs;
52
53         // Firefox before 67 runs extensions in incognito by default
54         // but we keep running after an update, so exclude those tabs for it
55         if (IS_FIREFOX) {
56             filteredTabs = filteredTabs.filter(function (tab) {
57                 return !tab.incognito;
58             });
59         }
60
61         var filteredTabs = filterArrayObjects(filteredTabs, whitelistedTabProperties);
62
63         // Shared between the callbacks
64         var total = filteredTabs.length;
65
66         var sendTabsIfComplete = function() {
67             if (--total > 0) {
68                 return;
69             }
70
71             port.postMessage({
72                 subsystem: "tabsrunner",
73                 event: "gotTabs",
74                 tabs: filteredTabs
75             });
76         };
77
78         for (let tabIndex in filteredTabs) {
79             let currentIndex = tabIndex; // Not shared
80             var favIconUrl = filteredTabs[tabIndex].favIconUrl;
81
82             if (!favIconUrl) {
83                 sendTabsIfComplete();
84             } else if (favIconUrl.match(/^data:image/)) {
85                 // Already a data URL
86                 filteredTabs[currentIndex].favIconData = favIconUrl;
87                 filteredTabs[currentIndex].favIconUrl = "";
88                 sendTabsIfComplete();
89             } else {
90                 // Send a request to fill the cache (=no timeout)
91                 let xhrForCache = new XMLHttpRequest();
92                 xhrForCache.open("GET", favIconUrl);
93                 xhrForCache.send();
94
95                 // Try to fetch from (hopefully) the cache (100ms timeout)
96                 let xhr = new XMLHttpRequest();
97                 xhr.onreadystatechange = function() {
98                     if (xhr.readyState != 4) {
99                         return;
100                     }
101
102                     if (!xhr.response) {
103                         filteredTabs[currentIndex].favIconData = "";
104                         sendTabsIfComplete();
105                         return;
106                     }
107
108                     var reader = new FileReader();
109                     reader.onloadend = function() {
110                         filteredTabs[currentIndex].favIconData = reader.result;
111                         sendTabsIfComplete();
112                     }
113                     reader.readAsDataURL(xhr.response);
114                 };
115                 xhr.open('GET', favIconUrl);
116                 xhr.responseType = 'blob';
117                 xhr.timeout = 100;
118                 xhr.send();
119             }
120         }
121     });
122 });