Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor code to modify the PREF cookie into a new function
Also include unit tests for the new function.
  • Loading branch information
klemens committed Oct 27, 2016
1 parent 00976ec commit 3664dd6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
29 changes: 29 additions & 0 deletions lib/CookieChanger.js
Expand Up @@ -4,6 +4,7 @@ const {Ci} = require("chrome");
// export CookieChanger
exports.CookieChanger = CookieChanger;
exports.Cookies = Cookies;
exports.modifyPrefCookie = modifyPrefCookie;

// save all CookieChangers
var listeners = [];
Expand Down Expand Up @@ -89,6 +90,34 @@ Cookies.prototype.set = function(key, value) {
}


/**
* Set single keys in the PREF cookie used by youtube to store settings
*
* @param cookies Cookies The cookies instance to modify
* @param key string The key to set or replace if existing, eg. "f2"
* @param value string The value for the given key
*/
function modifyPrefCookie(cookies, key, value) {
var entry = key + "=" + value;

if(cookies.has("PREF")) {
var pref = cookies.get("PREF");
var pattern = new RegExp(key + "=[^&]+");

if(pref.length == 0) {
pref = entry;
} else if(-1 == pref.search(pattern)) {
pref += "&" + entry;
} else {
pref = pref.replace(pattern, entry);
}
cookies.set("PREF", pref);
} else {
cookies.set("PREF", entry);
}
}


// Event listener that calls the other registered listeners based on
// the active state and the given hostname.
// The cookie header is only parsed when at least one callback is applicable.
Expand Down
17 changes: 2 additions & 15 deletions lib/main.js
Expand Up @@ -8,7 +8,7 @@ var tabs = require("sdk/tabs");
const {UserAgentChanger} = require("./UserAgentChanger");
const {URIChanger} = require("./URIChanger");
const {PluginPermissionChanger} = require("./PluginPermissionChanger");
const {CookieChanger} = require("./CookieChanger");
const {CookieChanger, modifyPrefCookie} = require("./CookieChanger");

// Internet Explorer 10 on Windows 7
const IEUserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
Expand All @@ -31,20 +31,7 @@ var youtubeTheaterMode = new CookieChanger("www.youtube.com", (cookies) => {
cookies.set("wide", "1");
});
var youtubeHTML5Test = new CookieChanger("www.youtube.com", (cookies) => {
if(cookies.has("PREF")) {
var pref = cookies.get("PREF");
var pattern = /f2=\d+/;
if(pref.length == 0) {
pref = "f2=40000000";
} else if(-1 == pref.search(pattern)) {
pref += "&f2=40000000";
} else {
pref = pref.replace(pattern, "f2=40000000");
}
cookies.set("PREF", pref);
} else {
cookies.set("PREF", "f2=40000000");
}
modifyPrefCookie(cookies, "f2", "40000000");
});

function createContextMenuEntry() {
Expand Down
18 changes: 18 additions & 0 deletions test/test-cookies.js
Expand Up @@ -25,4 +25,22 @@ exports.testCookieParsing = (assert) => {
assert.equal(t("a=1; abc; b=2"), "a=1; b=2");
};

exports.testModifyPref = (assert) => {
var t = (testcase, key, value) => {
var cookies = new Cookies(testcase);
modifyPrefCookie(cookies, key, value);
return cookies.unparse();
};

var t1 = "PREF=f1=abc&f2=999";
assert.equal(t(t1, "f1", "1234"), "PREF=f1=1234&f2=999");
assert.equal(t(t1, "f2", "abcd"), "PREF=f1=abc&f2=abcd");
assert.equal(t(t1, "f3", "123"), "PREF=f1=abc&f2=999&f3=123");
assert.equal(t(t1, "f1", ""), "PREF=f1=&f2=999");
assert.equal(t(t1, "f2", ""), "PREF=f1=abc&f2=");

assert.equal(t("PREF=", "f2", "1"), "PREF=f2=1");
assert.equal(t("", "f2", "1"), "PREF=f2=1");
};

require('sdk/test').run(exports);

0 comments on commit 3664dd6

Please sign in to comment.