From 64e1ef8dc3abfb32fec2db5fb67891a29cfe12ea Mon Sep 17 00:00:00 2001 From: Nicholas DiPiazza Date: Sat, 23 Jun 2018 01:18:08 -0500 Subject: [PATCH] fix race condition --- common.js | 1 + content.js | 48 +++++++++++++++++++++++++++++++++++++++++------- inject.js | 15 --------------- 3 files changed, 42 insertions(+), 22 deletions(-) delete mode 100644 inject.js diff --git a/common.js b/common.js index f2d6f0b..4301e13 100644 --- a/common.js +++ b/common.js @@ -15,5 +15,6 @@ var processCookieStr = function(cookiesStr) { }; var processSetCookieStr = function(str) { + console.log("Processing set cookie string " + str); return getPrefix()+str; }; \ No newline at end of file diff --git a/content.js b/content.js index b48fcf7..5f38a3c 100644 --- a/content.js +++ b/content.js @@ -1,10 +1,44 @@ -var headElement = (document.head||document.documentElement); +var actualCode = `var cookieGetter = document.__lookupGetter__("cookie").bind(document); +var cookieSetter = document.__lookupSetter__("cookie").bind(document); -var injectJs = function(fileName) { - var s = document.createElement('script'); - s.src = chrome.extension.getURL(fileName); - headElement.insertBefore(s, headElement.firstElementChild); +var getPrefix = function() { + return "oogi$" }; -injectJs("common.js"); -injectJs("inject.js"); +var processCookieStr = function(cookiesStr) { + var prefix = getPrefix(); + var cookieStrList = cookiesStr.split('; '); + var newStrList = []; + cookieStrList.forEach(function(cookieStr){ + if (cookieStr.indexOf(prefix)==0) { + newStrList.push(cookieStr.substring(prefix.length, cookieStr.length)); + } + }); + return newStrList.join("; "); +}; + +var processSetCookieStr = function(str) { + console.log("Processing set cookie string " + str); + return getPrefix()+str; +}; + +Object.defineProperty(document, 'cookie', { + get: function() { + var storedCookieStr = cookieGetter(); + console.log("Intercepted a cookie get " + storedCookieStr + " , and returning processed cookie string " + processCookieStr(storedCookieStr)); + return processCookieStr(storedCookieStr); + }, + + set: function(cookieString) { + var newValue = processSetCookieStr(cookieString); + console.log("Intercepted a cookie set " + newValue) + return cookieSetter(newValue); + } +}); +console.log("cookie get/set injector completed"); +`; + +var script = document.createElement('script'); +script.textContent = actualCode; +(document.head||document.documentElement).appendChild(script); +script.remove(); \ No newline at end of file diff --git a/inject.js b/inject.js deleted file mode 100644 index eecfc0d..0000000 --- a/inject.js +++ /dev/null @@ -1,15 +0,0 @@ -var cookieGetter = document.__lookupGetter__("cookie").bind(document); -var cookieSetter = document.__lookupSetter__("cookie").bind(document); - - -Object.defineProperty(document, 'cookie', { - get: function() { - var storedCookieStr = cookieGetter(); - return processCookieStr(storedCookieStr); - }, - - set: function(cookieString) { - var newValue = processSetCookieStr(cookieString); - return cookieSetter(newValue); - } -}); \ No newline at end of file