From f10e202a1e22a56299ed5f5902c4850e0eb9f56f Mon Sep 17 00:00:00 2001 From: Mihai Branescu Date: Thu, 23 Apr 2020 23:08:55 +0300 Subject: [PATCH] For #6557 - added web-extension for cookies --- .../main/assets/extensions/cookies/cookies.js | 47 +++++++++++++++++++ .../extensions/cookies/cookiesBackground.js | 28 +++++++++++ .../assets/extensions/cookies/manifest.json | 32 +++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 app/src/main/assets/extensions/cookies/cookies.js create mode 100644 app/src/main/assets/extensions/cookies/cookiesBackground.js create mode 100644 app/src/main/assets/extensions/cookies/manifest.json diff --git a/app/src/main/assets/extensions/cookies/cookies.js b/app/src/main/assets/extensions/cookies/cookies.js new file mode 100644 index 000000000..d5ac33bd2 --- /dev/null +++ b/app/src/main/assets/extensions/cookies/cookies.js @@ -0,0 +1,47 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +const COOKIES_CHECK_TIMEOUT_MS = 1000; + +function sendCookies(cookies) { + let message = { + 'url': document.location.href, + 'cookies': cookies + } + browser.runtime.sendNativeMessage("BrowserCookiesMessage", message); +} + +function notify(message) { + sendCookies(message.cookies); +} + +browser.runtime.onMessage.addListener(notify); + +const events = ["pageshow", "load", "unload"]; +var timeout; + +const eventLogger = event => { + switch (event.type) { + case "load": + timeout = setTimeout(() => { + browser.runtime.sendMessage({"checkCookies": true}); + }, COOKIES_CHECK_TIMEOUT_MS); + break; + case "pageshow": + if (event.persisted) { + timeout = setTimeout(() => { + browser.runtime.sendMessage({"checkCookies": true}); + }, COOKIES_CHECK_TIMEOUT_MS); + } + break; + case "unload": + clearTimeout(timeout); + default: + console.log('Event:', event.type); + } +}; + +events.forEach(eventName => + window.addEventListener(eventName, eventLogger) +); diff --git a/app/src/main/assets/extensions/cookies/cookiesBackground.js b/app/src/main/assets/extensions/cookies/cookiesBackground.js new file mode 100644 index 000000000..755eb6909 --- /dev/null +++ b/app/src/main/assets/extensions/cookies/cookiesBackground.js @@ -0,0 +1,28 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +browser.runtime.onMessage.addListener(notify); + +function sendMessageToTabs(tabs, cookies) { + for (let tab of tabs) { + browser.tabs.sendMessage( + tab.id, + {cookies: cookies} + ); + } +} + +function notify(message) { + if(message.checkCookies) { + browser.cookies.getAll({}) + .then(cookies => { + browser.tabs.query({ + currentWindow: true, + active: true + }).then(tabs => { + sendMessageToTabs(tabs, cookies); + }); + }); + } +} \ No newline at end of file diff --git a/app/src/main/assets/extensions/cookies/manifest.json b/app/src/main/assets/extensions/cookies/manifest.json new file mode 100644 index 000000000..dd56d6ea2 --- /dev/null +++ b/app/src/main/assets/extensions/cookies/manifest.json @@ -0,0 +1,32 @@ +{ + "manifest_version": 2, + "name": "Mozilla Android Components - Cookies", + "version": "1.0", + "content_scripts": [ + { + "matches": ["https://*/*"], + "include_globs": [ + "https://www.google.*/search*", + "https://www.baidu.com/from=844b/s*", + "https://www.baidu.com/from=844b/baidu*", + "https://*search.yahoo.com/search*", + "https://www.bing.com/search*", + "https://duckduckgo.com/*" + ], + "js": ["cookies.js"], + "run_at": "document_end" + } + ], + "background": { + "scripts": ["cookiesBackground.js"] + }, + "permissions": [ + "geckoViewAddons", + "nativeMessaging", + "webNavigation", + "webRequest", + "webRequestBlocking", + "cookies", + "*://*/*" + ] +}