Skip to content
Snippets Groups Projects
Commit 68041426 authored by Anton Bershanskiy's avatar Anton Bershanskiy
Browse files

Rudimentary message passing

Now can pass messages between webpage, popup and background.
parent b61a2454
No related branches found
No related tags found
No related merge requests found
"use strict"; "use strict"
// Initialize the cookie database upon extension installation // Initialize the cookie database upon extension installation
const platform = chrome; const platform = chrome
const urlPopup = window.location.origin + "/pages/popup/popup.html"
// TODO: make an actual datasructure. // TODO: make an actual datasructure.
var cookieDatabase = {} var cookieDatabase = {}
...@@ -28,6 +30,24 @@ platform.runtime.onInstalled.addListener (function() { ...@@ -28,6 +30,24 @@ platform.runtime.onInstalled.addListener (function() {
if (result) if (result)
onPermissionWebRequestGranted() onPermissionWebRequestGranted()
}) })
// Listen for incomming messages form other pages (popup)
platform.runtime.onMessage.addListener(function(message, sender/*, sendResponse*/){
/* sender.envType takes on useful values "content_child" and "addon_child" */
switch (sender.url){
case urlPopup:
if(message.popupOpen === true) {
console.log("Popup open", sender, message)
// sendResponse(cookieDatabase)
}
if(message.popupOpen === false) {
console.log("Popup closed", sender, message)
}
break
default:
console.log("Unknown message", message, sender)
}
})
}) })
...@@ -70,7 +90,7 @@ function onPermissionWebRequestGranted(){ ...@@ -70,7 +90,7 @@ function onPermissionWebRequestGranted(){
// TODO: Difference between "set-cookie" and "Set-Cookie" // TODO: Difference between "set-cookie" and "Set-Cookie"
// TODO: support protocols other than HTTP(S) // TODO: support protocols other than HTTP(S)
function watchCookiesSet(details){ function watchCookiesSet(details){
// console.log(details) //console.log(details)
// Get additional parameters // Get additional parameters
const protocol = details.url.substring(0, details.url.indexOf("://")) const protocol = details.url.substring(0, details.url.indexOf("://"))
...@@ -80,7 +100,7 @@ function onPermissionWebRequestGranted(){ ...@@ -80,7 +100,7 @@ function onPermissionWebRequestGranted(){
const name = value.substring(0, value.indexOf("=")) const name = value.substring(0, value.indexOf("="))
console.log("Cookie set: ", name) console.log("Cookie set: ", name)
cookieDatabase[name] = {secureOrigin: protocol === "https", httpOrigin: true} cookieDatabase[name] = {secureOrigin: protocol === "https", httpOnly: true}
} }
} /*else { } /*else {
console.log(details.responseHeaders[i].name) console.log(details.responseHeaders[i].name)
...@@ -88,11 +108,17 @@ function onPermissionWebRequestGranted(){ ...@@ -88,11 +108,17 @@ function onPermissionWebRequestGranted(){
return {responseHeaders: details.responseHeaders} return {responseHeaders: details.responseHeaders}
} }
function logError(details){
console.log("Network error, e.g. other extension blocked")
}
platform.webRequest.onBeforeSendHeaders.addListener (watchCookiesSent, platform.webRequest.onBeforeSendHeaders.addListener (watchCookiesSent,
{urls: urls}, ["blocking", "requestHeaders"]) {urls: urls}, ["blocking", "requestHeaders"])
platform.webRequest.onHeadersReceived.addListener (watchCookiesSet, platform.webRequest.onHeadersReceived.addListener (watchCookiesSet,
{urls: urls}, ["blocking", "responseHeaders"]) {urls: urls}, ["blocking", "responseHeaders"])
platform.webRequest.onErrorOccurred.addListener(logError, {urls: urls})
} }
function recordCookieChange(/*object*/ changeInfo){ function recordCookieChange(/*object*/ changeInfo){
......
...@@ -2,8 +2,15 @@ ...@@ -2,8 +2,15 @@
(function() { (function() {
// Execute the actal payload in the context of the window // Execute the actal payload in the context of the window
var s = document.createElement("script"); var s = document.createElement("script")
s.src = chrome.extension.getURL("/web_accessible_resources/inject.js"); s.src = chrome.extension.getURL("/web_accessible_resources/inject.js")
(document.head || document.documentElement).appendChild(s); (document.head || document.documentElement).appendChild(s)
s.parentNode.removeChild(s); s.parentNode.removeChild(s)
})() })()
\ No newline at end of file
document.addEventListener("cookie_message", function(event) {
// We only accept messages from ourselves
// if (event.source != window)
// return
chrome.runtime.sendMessage(event.detail)
})
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<p>This setting works similarly to the Chromium content settings. This is just the list of "origins" specified in the permissions. Simply put, the extension will only run on the pages matching at least one expression:</p> <p>This setting works similarly to the Chromium content settings. This is just the list of "origins" specified in the permissions. Simply put, the extension will only run on the pages matching at least one expression:</p>
<form id="newOriginForm"> <form id="newOriginForm">
<label for="newOrigin">New origin</label> <label for="newOrigin">New origin</label>
<input type="url" id="newOrigin" placeholder="https://example.com/" required> <input type="text" id="newOrigin" placeholder="https://example.com/" required>
<input type="submit" value="Add origin"> <input type="submit" value="Add origin">
</form> </form>
<p>TODO: for some reasson, domains have to end with slash.</p> <p>TODO: for some reasson, domains have to end with slash.</p>
......
"use strict"; "use strict";
// Notify other pages (background) that popup is open
platform.runtime.sendMessage({popupOpen: true})
/*
TODO: uncomment and figure out source of warnings
window.addEventListener("unload", function(evt){
platform.runtime.sendMessage({popupOpen: false})
return true
})*/
const mainMenu = document.getElementById("main-menu") const mainMenu = document.getElementById("main-menu")
const mainMenuMonitoring = document.getElementById ("main-menu-monitoring") const mainMenuMonitoring = document.getElementById ("main-menu-monitoring")
...@@ -21,6 +31,7 @@ function debugMessage(message){ ...@@ -21,6 +31,7 @@ function debugMessage(message){
document.body.appendChild(div) document.body.appendChild(div)
} }
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
mainMenu.addEventListener("click", function(evt) { mainMenu.addEventListener("click", function(evt) {
......
// This code is executed in the context of the window // This code is executed in the context of the window
var injectionSuccessful = true; var injectionSuccessful = true
console.log("Injection completed, try logging the variable injectionSuccessful"); console.log("Injection completed, try logging the variable injectionSuccessful")
\ No newline at end of file
function sendMessage(message){
var event = new CustomEvent("cookie_message", {detail: message})
document.dispatchEvent(event)
}
/*
* Function processing cookie during retreival
*/
function processCookieString(cookieString){
console.log("Cookie read: ", cookieString)
sendMessage({"event": "read"})
return cookieString
}
/*
* Function processing cookie during assignment
*/
function processSetCookieStr(cookieString){
console.log("Cookie set: ", cookieString)
sendMessage({"event": "write", "cookie": cookieString})
return cookieString
}
/*
* Wedge custom cookie handlers in regular setters and getters
*/
var cookieGetter = document.__lookupGetter__("cookie").bind(document)
var cookieSetter = document.__lookupSetter__("cookie").bind(document)
Object.defineProperty (document, "cookie", {
get: function() {
return processCookieString (cookieGetter())
},
set: function(cookieString) {
return cookieSetter (processSetCookieStr(cookieString))
}
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment