Debian Codegraph @

src/js/contentscripts/supercookie.js

raw
1 /*
2 * This file is part of Privacy Badger <https://privacybadger.org/>
3 * Copyright (C) 2015 Electronic Frontier Foundation
4 *
5 * Derived from Chameleon <https://github.com/ghostwords/chameleon>
6 * Copyright (C) 2015 ghostwords
7 *
8 * Privacy Badger is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
11 *
12 * Privacy Badger is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 (function () {
22
23 // don't inject into non-HTML documents (such as XML documents)
24 // but do inject into XHTML documents
25 if (document instanceof HTMLDocument === false && (
26 document instanceof XMLDocument === false ||
27 document.createElement('div') instanceof HTMLDivElement === false
28 )) {
29 return;
30 }
31
32 // don't bother asking to run when trivially in first-party context
33 if (window.top == window) {
34 return;
35 }
36
37 /**
38 * Generate script to inject into the page
39 *
40 * @returns {string}
41 */
42 function getPageScript(event_id) {
43 // code below is not a content script: no chrome.* APIs /////////////////////
44
45 // return a string
46 return "(" + function (EVENT_ID) {
47
48 /*
49 * If localStorage is inaccessible, such as when "Block third-party cookies"
50 * in enabled in Chrome or when `dom.storage.enabled` is set to `false` in
51 * Firefox, do not go any further.
52 */
53 try {
54 // No localStorage raises an Exception in Chromium-based browsers, while
55 // it's equal to `null` in Firefox.
56 if (null === localStorage) {
57 throw false;
58 }
59 } catch (ex) {
60 return;
61 }
62
63 (function (DOCUMENT, dispatchEvent, CUSTOM_EVENT, LOCAL_STORAGE, OBJECT, keys) {
64
65 function send(message) {
66 dispatchEvent.call(DOCUMENT, new CUSTOM_EVENT(EVENT_ID, {
67 detail: message
68 }));
69 }
70
71 /**
72 * Read HTML5 local storage and return contents
73 * @returns {Object}
74 */
75 function getLocalStorageItems() {
76 let lsItems = {};
77 for (let i = 0; i < LOCAL_STORAGE.length; i++) {
78 let key = LOCAL_STORAGE.key(i);
79 lsItems[key] = LOCAL_STORAGE.getItem(key);
80 }
81 return lsItems;
82 }
83
84 let localStorageItems = getLocalStorageItems();
85 if (keys.call(OBJECT, localStorageItems).length) {
86 send({ localStorageItems });
87 }
88
89 // save locally to keep from getting overwritten by site code
90 } (document, document.dispatchEvent, CustomEvent, localStorage, Object, Object.keys));
91
92 } + "(" + event_id + "));";
93
94 // code above is not a content script: no chrome.* APIs /////////////////////
95
96 }
97
98 // END FUNCTION DEFINITIONS ///////////////////////////////////////////////////
99
100 // TODO race condition; fix waiting on https://crbug.com/478183
101
102 // TODO here we could also be injected too quickly
103 // and miss localStorage setting upon initial page load
104 //
105 // we should eventually switch injection back to document_start
106 // (reverting https://github.com/EFForg/privacybadger/pull/1522),
107 // and fix localstorage detection
108 // (such as by delaying it or peforming it periodically)
109 //
110 // could then remove test workarounds like
111 // https://github.com/EFForg/privacybadger/commit/39d5d0899e22d1c451d429e44553c5f9cad7fc46
112
113 // TODO sometimes contentscripts/utils.js isn't here?!
114 // TODO window.FRAME_URL / window.injectScript are undefined ...
115 chrome.runtime.sendMessage({
116 type: "detectSupercookies",
117 frameUrl: window.FRAME_URL
118 }, function (enabledAndThirdParty) {
119 if (!enabledAndThirdParty) {
120 return;
121 }
122
123 const event_id = Math.random();
124
125 // listen for messages from the script we are about to insert
126 document.addEventListener(event_id, function (e) {
127 // pass these on to the background page (handled by webrequest.js)
128 chrome.runtime.sendMessage({
129 type: "supercookieReport",
130 data: e.detail,
131 frameUrl: window.FRAME_URL
132 });
133 });
134
135 window.injectScript(getPageScript(event_id));
136
137 });
138
139 }());