summaryrefslogtreecommitdiffstats
path: root/src/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'src/scripts')
-rw-r--r--src/scripts/about.js26
-rw-r--r--src/scripts/background.js275
-rw-r--r--src/scripts/common.js63
-rw-r--r--src/scripts/import-proxy-list.js240
-rw-r--r--src/scripts/import.js440
-rw-r--r--src/scripts/jscolor-2.0.5.js1855
-rw-r--r--src/scripts/log.js151
-rw-r--r--src/scripts/matcher.js116
-rw-r--r--src/scripts/options.js380
-rw-r--r--src/scripts/pattern-help.js9
-rw-r--r--src/scripts/pattern-tester.js77
-rw-r--r--src/scripts/patterns.js283
-rw-r--r--src/scripts/popup.js112
-rw-r--r--src/scripts/proxy.js228
-rw-r--r--src/scripts/utils.js324
15 files changed, 4579 insertions, 0 deletions
diff --git a/src/scripts/about.js b/src/scripts/about.js
new file mode 100644
index 0000000..872a829
--- /dev/null
+++ b/src/scripts/about.js
@@ -0,0 +1,26 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ location.href = '/options.html';
+ }
+});
+
+const manifest = chrome.runtime.getManifest();
+document.querySelector('#version').textContent = manifest.version;
+document.querySelector('#edition').textContent = 'FoxyProxy ' + (FOXYPROXY_BASIC ? 'Basic' : 'Standard');
+document.querySelector('button').addEventListener('click', () => location.href = '/options.html');
+
+// --- remove nodes completely for FP Basic
+FOXYPROXY_BASIC && document.querySelectorAll('.notForBasic').forEach(item => item.remove());
+
+// --- welcome on install/update
+location.search === '?welcome' && document.querySelector('.welcome').classList.remove('hide');
diff --git a/src/scripts/background.js b/src/scripts/background.js
new file mode 100644
index 0000000..a60546b
--- /dev/null
+++ b/src/scripts/background.js
@@ -0,0 +1,275 @@
+'use strict';
+
+// ----- global
+//const FF = typeof browser !== 'undefined'; // for later
+let storageArea; // keeping track of sync
+let bgDisable = false;
+
+// Start in disabled mode because it's going to take time to load setings from storage
+let activeSettings = {mode: 'disabled'};
+
+// ----------------- logger --------------------------------
+let logger;
+function getLog() { return logger; }
+class Logger {
+
+ constructor(size = 100, active = false) {
+ this.size = size;
+ this.matchedList = [];
+ this.unmatchedList = [];
+ this.active = active;
+ }
+
+ clear() {
+ this.matchedList = [];
+ this.unmatchedList = [];
+ }
+
+ addMatched(item) {
+ this.matchedList.push(item);
+ this.matchedList = this.matchedList.slice(-this.size); // slice to the ending size entries
+ }
+
+ addUnmatched(item) {
+ this.unmatchedList.push(item);
+ this.unmatchedList = this.unmatchedList.slice(-this.size); // slice to the ending size entries
+ }
+
+ updateStorage() {
+ this.matchedList = this.matchedList.slice(-this.size); // slice to the ending size entries
+ this.unmatchedList = this.unmatchedList.slice(-this.size); // slice to the ending size entries
+ storageArea.set({logging: {size: this.size, active: this.active} });
+ }
+}
+// ----------------- /logger -------------------------------
+
+// --- registering persistent listener
+// https://bugzilla.mozilla.org/show_bug.cgi?id=1359693 ...Resolution: --- ? WONTFIX
+chrome.webRequest.onAuthRequired.addListener(sendAuth, {urls: ['*://*/*']}, ['blocking']);
+chrome.webRequest.onCompleted.addListener(clearPending, {urls: ['*://*/*']});
+chrome.webRequest.onErrorOccurred.addListener(clearPending, {urls: ['*://*/*']});
+
+chrome.runtime.onInstalled.addListener((details) => { // Installs Update Listener
+ // reason: install | update | browser_update | shared_module_update
+ switch (true) {
+
+ case details.reason === 'install':
+ case details.reason === 'update' && /^(3\.|4\.|5\.5|5\.6)/.test(details.previousVersion):
+ chrome.tabs.create({url: '/about.html?welcome'});
+ break;
+ }
+});
+
+// ----------------- User Preference -----------------------
+chrome.storage.local.get(null, result => {
+ // browserVersion is not used & runtime.getBrowserInfo() is not supported on Chrome
+ // sync is NOT set or it is false, use this result ELSE get it from storage.sync
+ // check both storage on start-up
+ if (!Object.keys(result)[0]) { // local is empty, check sync
+
+ chrome.storage.sync.get(null, syncResult => {
+ if (!Object.keys(syncResult)[0]) { // sync is also empty
+ storageArea = chrome.storage.local; // set storage as local
+ process(result);
+ }
+ else {
+ chrome.storage.local.set({sync: true}); // save sync as true
+ storageArea = chrome.storage.sync; // set storage as sync
+ process(syncResult);
+ }
+ });
+ }
+ else {
+ storageArea = result.sync ? chrome.storage.sync : chrome.storage.local; // cache for subsequent use
+ !result.sync ? process(result) : chrome.storage.sync.get(null, process);
+ }
+});
+// ----------------- /User Preference ----------------------
+
+function process(settings) {
+
+ let update;
+ let prefKeys = Object.keys(settings);
+
+ if (!settings || !prefKeys[0]) { // create default settings if there are no settings
+ // default
+ settings = {
+ mode: 'disabled',
+ logging: {
+ size: 100,
+ active: false
+ }
+ };
+ update = true;
+ }
+
+ // update storage then add Change Listener
+ if (update) {
+ storageArea.set(settings, () => chrome.storage.onChanged.addListener(storageOnChanged));
+ }
+ else {
+ chrome.storage.onChanged.addListener(storageOnChanged);
+ }
+
+ logger = settings.logging ? new Logger(settings.logging.size, settings.logging.active) : new Logger();
+ setActiveSettings(settings);
+ console.log('background.js: loaded proxy settings from storage.');
+}
+
+function storageOnChanged(changes, area) {
+// console.log(changes);
+ // update storageArea on sync on/off change from options
+ if (changes.hasOwnProperty('sync') && changes.sync.newValue !== changes.sync.oldValue) {
+ storageArea = changes.sync.newValue ? chrome.storage.sync : chrome.storage.local;
+ }
+
+ // update logger from log
+ if (Object.keys(changes).length === 1 && changes.logging) { return; }
+
+
+ // mode change from bg
+ if(changes.mode && changes.mode.newValue === 'disabled' && bgDisable) {
+ bgDisable = false;
+ return;
+ }
+
+ // default: changes from popup | options
+ storageArea.get(null, setActiveSettings);
+}
+
+function proxyRequest(requestInfo) {
+ return findProxyMatch(requestInfo.url, activeSettings);
+}
+
+function setActiveSettings(settings) {
+ browser.proxy.onRequest.hasListener(proxyRequest) && browser.proxy.onRequest.removeListener(proxyRequest);
+
+ const pref = settings;
+ const prefKeys = Object.keys(pref).filter(item => !['mode', 'logging', 'sync'].includes(item)); // not for these
+
+ // --- cache credentials in authData (only those with user/pass)
+ prefKeys.forEach(id => pref[id].username && pref[id].password &&
+ (authData[pref[id].address] = {username: pref[id].username, password: pref[id].password}) );
+
+ const mode = settings.mode;
+ activeSettings = { // global
+ mode,
+ proxySettings: []
+ };
+
+ if (mode === 'disabled' || (FOXYPROXY_BASIC && mode === 'patterns')){
+ setDisabled();
+ return;
+ }
+
+ if (['patterns', 'random', 'roundrobin'].includes(mode)) { // we only support 'patterns' ATM
+
+ // filter out the inactive proxy settings
+ prefKeys.forEach(id => pref[id].active && activeSettings.proxySettings.push(pref[id]));
+ activeSettings.proxySettings.sort((a, b) => a.index - b.index); // sort by index
+
+ function processPatternObjects(patternObjects) {
+ return patternObjects.reduce((accumulator, patternObject) => {
+ patternObject = Utils.processPatternObject(patternObject);
+ patternObject && accumulator.push(patternObject);
+ return accumulator;
+ }, []);
+ }
+
+ // Filter out the inactive patterns. that way, each comparison
+ // is a little faster (doesn't even know about inactive patterns). Also convert all patterns to reg exps.
+ for (const idx in activeSettings.proxySettings) {
+ activeSettings.proxySettings[idx].blackPatterns = processPatternObjects(activeSettings.proxySettings[idx].blackPatterns);
+ activeSettings.proxySettings[idx].whitePatterns = processPatternObjects(activeSettings.proxySettings[idx].whitePatterns);
+ }
+ browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});
+ Utils.updateIcon('images/icon.svg', null, 'patterns', true);
+ console.log(activeSettings, "activeSettings in patterns mode");
+ }
+ else {
+ // User has selected a proxy for all URLs (not patterns, disabled, random, round-robin modes).
+ // mode is set to the proxySettings id to use for all URLs.
+ if (settings[mode]) {
+ activeSettings.proxySettings = [settings[mode]];
+ browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});
+ const tmp = Utils.getProxyTitle(settings[mode]);
+ Utils.updateIcon('images/icon.svg', settings[mode].color, tmp, false, tmp, false);
+ console.log(activeSettings, "activeSettings in fixed mode");
+ }
+ else {
+ // This happens if user deletes the current proxy and mode is "use this proxy for all URLs"
+ // Don't remove this block.
+ bgDisable = true;
+ storageArea.set({mode: 'disabled'}); // only in case of error, otherwise mode is already set
+ setDisabled();
+ console.error(`Error: mode is set to ${mode} but no active proxySetting is found with that id. Disabling Due To Error`);
+ }
+ }
+}
+
+
+function setDisabled(isError) {
+ browser.proxy.onRequest.hasListener(proxyRequest) && browser.proxy.onRequest.removeListener(proxyRequest);
+ chrome.runtime.sendMessage({mode: 'disabled'}); // Update the options.html UI if it's open
+ Utils.updateIcon('images/icon-off.svg', null, 'disabled', true);
+ console.log('******* disabled mode');
+}
+
+
+// ----------------- Proxy Authentication ------------------
+// ----- session global
+let authData = {};
+let authPending = {};
+
+async function sendAuth(request) {
+ // Do nothing if this not proxy auth request:
+ // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onAuthRequired
+ // "Take no action: the listener can do nothing, just observing the request. If this happens, it will
+ // have no effect on the handling of the request, and the browser will probably just ask the user to log in."
+ if (!request.isProxy) return;
+
+ // --- already sent once and pending
+ if (authPending[request.requestId]) { return {cancel: true}; }
+
+ // --- authData credentials not yet populated from storage
+ if(!Object.keys(authData)[0]) { await getAuth(request); }
+
+ // --- first authentication
+ // According to https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onAuthRequired :
+ // "request.challenger.host is the requested host instead of the proxy requesting the authentication"
+ // But in my tests (Fx 69.0.1 MacOS), it is indeed the proxy requesting the authentication
+ // TODO: test in future Fx releases to see if that changes.
+ // console.log(request.challenger.host, "challenger host");
+ if (authData[request.challenger.host]) {
+ authPending[request.requestId] = 1; // prevent bad authentication loop
+ return {authCredentials: authData[request.challenger.host]};
+ }
+ // --- no user/pass set for the challenger.host, leave the authentication to the browser
+}
+
+async function getAuth(request) {
+
+ await new Promise(resolve => {
+ chrome.storage.local.get(null, result => {
+ const host = result.hostData[request.challenger.host];
+ if (host && host.username) { // cache credentials in authData
+ authData[host] = {username: host.username, password: host.password};
+ }
+ resolve();
+ });
+ });
+}
+
+function clearPending(request) {
+
+ if(!authPending[request.requestId]) { return; }
+
+ if (request.error) {
+ const host = request.proxyInfo && request.proxyInfo.host ? request.proxyInfo.host : request.ip;
+ Utils.notify(chrome.i18n.getMessage('authError', host));
+ console.error(request.error);
+ return; // auth will be sent again
+ }
+
+ delete authPending[request.requestId]; // no error
+} \ No newline at end of file
diff --git a/src/scripts/common.js b/src/scripts/common.js
new file mode 100644
index 0000000..bd516c8
--- /dev/null
+++ b/src/scripts/common.js
@@ -0,0 +1,63 @@
+'use strict';
+
+// ----------------- Pattern Check ------------------
+
+function checkPattern(pattern, type) {
+
+ const pat = pattern.value;
+
+ if (!pat) {
+ pattern.classList.add('invalid');
+ pattern.focus();
+ showResult(chrome.i18n.getMessage('errorEmpty'), true);
+ return;
+ }
+
+ const patternTypeSet = {
+ '1': 'wildcard',
+ '2': 'regex'
+ }
+
+ let regex;
+
+ switch (patternTypeSet[type.value]) {
+
+ // RegEx
+ case 'regex':
+ try { regex = new RegExp(pat); }
+ catch (e) {
+ pattern.classList.add('invalid');
+ showResult(e.message, true);
+ return false;
+ }
+ break;
+
+ // wildcard
+ default:
+ if (pat.includes('/')) {
+ pattern.classList.add('invalid');
+ showResult(chrome.i18n.getMessage('errorSlash'), true);
+ return false;
+ }
+
+ try { regex = new RegExp(Utils.wildcardToRegExp(pat)); }
+ catch (e) {
+ pattern.classList.add('invalid');
+ showResult(e.message, true);
+ return false;
+ }
+ }
+
+ // --- pattern is valid
+ return regex;
+}
+
+
+
+
+function showResult(text, fail) {
+
+ fail && result.classList.add('alert');
+ result.textContent = text;
+ result.classList.remove('hide');
+} \ No newline at end of file
diff --git a/src/scripts/import-proxy-list.js b/src/scripts/import-proxy-list.js
new file mode 100644
index 0000000..a8bd838
--- /dev/null
+++ b/src/scripts/import-proxy-list.js
@@ -0,0 +1,240 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ location.href = '/options.html';
+ }
+});
+
+// ----------------- Spinner -------------------------------
+const spinner = document.querySelector('.spinner');
+function hideSpinner() {
+
+ spinner.classList.remove('on');
+ setTimeout(() => { spinner.style.display = 'none'; }, 600);
+}
+
+function showSpinner() {
+
+ spinner.style.display = 'flex';
+ spinner.classList.add('on');
+}
+// ----------------- /spinner ------------------------------
+document.addEventListener('DOMContentLoaded', () => {
+ hideSpinner();
+});
+
+// addEventListener for all buttons & handle together
+document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));
+
+let proxiesAdded = 0; // Global to this module in case user does multiple bulk imports before closing import-bulk.html
+
+function process(e) {
+ switch (this.id || this.dataset.i18n) {
+ case 'back': location.href = '/options.html'; break;
+ case 'import': imp0rt(); break;
+ }
+}
+
+function imp0rt() {
+ const {parsedList, skippedList} = parseList(document.getElementById('proxyList').value);
+ if (parsedList.length > 0) {
+ if (document.querySelector('#overwrite').checked) {
+ if (confirm(chrome.i18n.getMessage('confirmOverwrite'))) {
+ showSpinner();
+ chrome.storage.local.clear(() => chrome.storage.sync.clear(() => {
+ hideSpinner();
+ storeProxies(parsedList);
+ }));
+ }
+ else {
+ return;
+ }
+ }
+ else {
+ storeProxies(parsedList);
+ }
+ }
+ if (skippedList.length > 0) {
+ alert(`${chrome.i18n.getMessage('importsSkipped', [skippedList.length + "", skippedList.toString()])}`);
+ }
+ if (parsedList.length > 0) {
+ alert(`${chrome.i18n.getMessage('importSucceeded', [parsedList.length])}`);
+ }
+ location.href = '/options.html';
+}
+
+function parseList(rawList) {
+ const parsedList = [], skippedList = [], colors = ['#663300', '#284B63', '#C99656', '#7B758C', '#171E1D'];
+ if (!rawList) {
+ return {parsedList, skippedList};
+ }
+ rawList.split('\n').forEach((item) => {
+ if (!item) {
+ return; // continue to next
+ }
+ let p, patternIncludesAll = true, patternExcludesIntranet = true;
+ // Is this line simple or complete format?
+ let protocol = item.match(/.+:\/\//); // null for strings like 127.0.0.1:3128 (simple format)
+ if (protocol) {
+ // This line is uses 'complete' format
+ let url;
+ try {
+ // In Firefox 78.0.2, the built-in javascript URL class will not parse URLs with custom schemes/protocols
+ // like socks://127.0.0.1. However, Chrome 84.0.4147.89 and Node 14.5.0 both do. In order to be compatible
+ // with Firefox, let's replace the scheme/protocol with 'http'. We could also instead write our own parsing
+ // logic with a regular expression, but that does not seems necessary.
+ if (protocol[0] !== 'http://' && protocol[0] !== 'https://') {
+ item = 'http://' + item.substring(protocol[0].length);
+ url = new URL(item);
+ protocol = protocol[0].substring(0, protocol[0].length-2); //strip ending //
+ }
+ else {
+ url = new URL(item);
+ protocol = url.protocol;
+ }
+ }
+ catch (e) {
+ console.log(e);
+ // URL couldn't be parsed
+ skippedList.push(item);
+ return; // continue to next
+ }
+ const type = protocol === 'proxy:' || protocol === 'http:' ? PROXY_TYPE_HTTP :
+ protocol === 'ssl:' || protocol === 'https:' ? PROXY_TYPE_HTTPS :
+ protocol === 'socks:' || protocol === 'socks5:' ? PROXY_TYPE_SOCKS5 :
+ protocol === 'socks4:' ? PROXY_TYPE_SOCKS4 : -1;
+ if (type === -1) {
+ console.log("unknown protocol");
+ skippedList.push(item);
+ return; // continue to next
+ }
+
+ // If color not specified in the URL, then rotate among the ones in the colors array.
+ const color = url.searchParams.get('color') ?
+ ('#' + url.searchParams.get('color')) : colors[parsedList.length % colors.length];
+
+ const title = url.searchParams.get('title');
+ const countryCode = url.searchParams.get('countryCode') || url.searchParams.get('cc');
+ const country = url.searchParams.get('country') || countryCode;
+
+ // If paramName url param is not specified or it's specified and not 'false', then paramValue should equal true.
+ // We assume true in case the param is absent, which may be counterintuitive, but this fcn is used for params that
+ // we want to assume true when absent.
+ function parseBooleanParam(url, paramName, aliasParamName) {
+ const paramValue = url.searchParams.get(paramName) || (aliasParamName && url.searchParams.get(aliasParamName));
+ return paramValue ? !(paramValue.toLowerCase() === 'false') : true;
+ }
+ const proxyDNS = parseBooleanParam(url, 'proxyDns');
+ const active = parseBooleanParam(url, 'enabled', 'active');
+
+ patternIncludesAll = parseBooleanParam(url, 'patternIncludesAll');
+ patternExcludesIntranet = parseBooleanParam(url, 'patternExcludesIntranet');
+
+ // the URL class sets port === '' if not specified on the URL or it's an invalid port e.g. contains alpha chars
+ let port = url.port;
+ if (port === '') {
+ // Default ports are 3128 for HTTP proxy, 443 for tls/ssl/https proxy, 1080 for socks4/5
+ port = type === PROXY_TYPE_HTTP ? 3128 : type === PROXY_TYPE_HTTPS ? 443 : 1080;
+ }
+
+ console.log(url);
+ // the URL class sets username and password === '' if not specified on the URL
+ p = {type, username: url.username, password: url.password, address: url.hostname, port, color, title, proxyDNS, active, countryCode, country};
+ }
+ else {
+ // simple
+ const splitItem = item.split(':');
+ // Split always returns an array no matter what
+ p = {address: splitItem[0], port: splitItem[1], username: splitItem[2], password: splitItem[3], color: colors[parsedList.length % colors.length]};
+ }
+
+ const proxy = makeProxy(p, patternIncludesAll, patternExcludesIntranet);
+ if (proxy) {
+ parsedList.push(proxy);
+ }
+ else {
+ skippedList.push(item);
+ }
+
+ }); //forEach
+
+ return {parsedList, skippedList};
+}
+
+function makeProxy({type = PROXY_TYPE_HTTP, username, password, address, port, color, title, proxyDNS, active = true, countryCode, country},
+ patternIncludesAll, patternExcludesIntranet) {
+
+ port = port*1; // convert to digit
+ if (!port || port < 1) { // is port NaN or less than 1
+ console.log("port is NaN or less than 1");
+ return null;
+ }
+
+ // strip bad chars from all input except username, password, type, proxyDNS, and active
+ // (those last 3 are forced to boolean types before we are called)
+ // If we do strip bad chars from usernams or password, auth could fail.
+ address = Utils.stripBadChars(address);
+ color = Utils.stripBadChars(color);
+ title = Utils.stripBadChars(title);
+ countryCode = Utils.stripBadChars(countryCode);
+ country = Utils.stripBadChars(country);
+
+ if (!address) {
+ console.log("no address");
+ return null;
+ }
+
+ const proxy = {type, address, port, color, active};
+
+ // Only set the properties needed. null and undefined props seem to be saved if set, so don't set them.
+ function setPropertyIfHasValue(prop, value, proxy) {
+ if (value || value === 0) {
+ proxy[prop] = value;
+ }
+ }
+ setPropertyIfHasValue('username', username, proxy);
+ setPropertyIfHasValue('password', password, proxy);
+ setPropertyIfHasValue('title', title, proxy);
+ setPropertyIfHasValue('cc', countryCode, proxy);
+ setPropertyIfHasValue('country', country, proxy);
+
+ if (type === PROXY_TYPE_SOCKS5) {
+ // Only set if socks5
+ proxy.proxyDNS = proxyDNS;
+ }
+
+ if (FOXYPROXY_BASIC) {
+ proxy.whitePatterns = proxy.blackPatterns = [];
+ }
+ else {
+ proxy.whitePatterns = patternIncludesAll ? [PATTERN_ALL_WHITE] : [];
+ proxy.blackPatterns = patternExcludesIntranet ? [...blacklistSet] : [];
+ }
+ return proxy;
+}
+
+function storeProxies(parsedList) {
+ const sync = localStorage.getItem('sync') === 'true';
+ const storageArea = !sync ? chrome.storage.local : chrome.storage.sync;
+
+ for (const idx in parsedList) {
+ const proxy = parsedList[idx];
+ console.log(proxy);
+ // Get the nextIndex given to us by options.js and add by the number of proxies we've added.
+ // This ensures this proxy setting is last in list of all proxy settings.
+
+ proxy.index = (localStorage.getItem('nextIndex')) + (++proxiesAdded);
+ storageArea.set({[Utils.getUniqueId()]: proxy}, () => {
+ console.log(`stored proxy`);
+ });
+ }
+}
diff --git a/src/scripts/import.js b/src/scripts/import.js
new file mode 100644
index 0000000..17ec6f5
--- /dev/null
+++ b/src/scripts/import.js
@@ -0,0 +1,440 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ close();
+ }
+});
+
+// ----------------- Spinner -------------------------------
+const spinner = document.querySelector('.spinner');
+function hideSpinner() {
+
+ spinner.classList.remove('on');
+ setTimeout(() => { spinner.style.display = 'none'; }, 600);
+}
+
+function showSpinner() {
+
+ spinner.style.display = 'flex';
+ spinner.classList.add('on');
+}
+// ----------------- /spinner ------------------------------
+hideSpinner();
+
+// addEventListener for all buttons & handle together
+document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));
+document.querySelectorAll('input[type="file"]').forEach(item => item.addEventListener('change', process));
+
+function process(e) {
+
+ switch (this.id || this.dataset.i18n) {
+ // click
+ case 'back': close(); break;
+ case 'export': Utils.exportFile(); break;
+
+ case 'togglePW|title':
+ const inp = this.previousElementSibling;
+ inp.type = inp.type === 'password' ? 'text' : 'password';
+ break;
+
+ // change
+ case 'importFP':
+ showSpinner();
+ foxyProxyImport();
+ break;
+
+ case 'importJson':
+ showSpinner();
+ Utils.importFile(e.target.files[0], ['application/json'], 1024*1024*10, 'json', importJson); // 10mb
+ hideSpinner(); // hide spinner in case importJson() was not called due to error
+ break;
+ case 'importXml':
+ showSpinner();
+ Utils.importFile(e.target.files[0], ['text/xml'], 1024*1024*10, 'xml', importXml); // 10mb
+ hideSpinner(); // hide spinner in case importXml() was not called due to error
+ break;
+ }
+}
+
+function importJson(result) {
+
+ if (!result) { // user cancelled
+ hideSpinner();
+ return;
+ }
+
+ // --- convert pre v7.0 export to db format
+ if (result.hasOwnProperty('proxySettings')) {
+ result = prepareForStorage(result);
+ }
+
+ save(result, end);
+}
+
+function save(result, callback) {
+
+ // Remove 'browserVersion', 'foxyProxyVersion', 'foxyProxyEdition' if they exist
+ // We don't need those imported.
+ delete result.browserVersion;
+ delete result.foxyProxyVersion;
+ delete result.foxyProxyEdition;
+
+ const storageArea = result.sync ? chrome.storage.sync : chrome.storage.local;
+
+ // clear the storages and set new
+ chrome.storage.local.clear(() => chrome.storage.sync.clear(() => {
+
+ if (result.sync) {
+ chrome.storage.local.set({sync: true}); // save sync state
+ delete result.sync;
+ }
+
+ storageArea.set(result, callback); // save to target
+ }));
+}
+
+
+function end() {
+ hideSpinner();
+ Utils.notify(chrome.i18n.getMessage('importEnd'));
+ location.href = '/options.html';
+}
+
+
+function importXml(doc) {
+
+ let lastResortFound = false;
+ // base format
+ const pref = {
+ mode: 'disabled',
+ logging: {
+ size: 100,
+ active: false
+ }
+ };
+
+ const FP = doc.querySelector('foxyproxy');
+ if (!FP) {
+ // Don't use Utils.notify() because at least on macOS,
+ // the message is too long and cut off
+ alert('There is an error with the XML file (missing <foxyproxy ....>)');
+ hideSpinner();
+ return;
+ }
+
+ const mode = FP.getAttribute('mode');
+ mode && (pref.mode = mode);
+
+ const badModes = [];
+
+ const proxies = doc.getElementsByTagName('proxy');
+ let patternsEdited = false;
+
+ const LASTRESORT = 'k20d21508277536715';
+ const DEFAULT_PROXY_SETTING = {
+ index: Number.MAX_SAFE_INTEGER,
+ id: LASTRESORT,
+ active: true,
+ title: 'Default',
+ notes: 'These are the settings that are used when no patterns match a URL.',
+ color: '#0055E5',
+ type: PROXY_TYPE_NONE,
+ whitePatterns: [PATTERN_ALL_WHITE],
+ blackPatterns: []
+ };
+
+ doc.querySelectorAll('proxy').forEach((item, index) => {
+
+ const proxy = {};
+ // type a.k.a. mode
+ const oldType = item.getAttribute('mode');
+ // Deactivate from patterns mode any unsupported types/modes
+ const allowedType = ['manual', 'direct'].includes(oldType);
+ proxy.active = allowedType ? item.getAttribute('enabled') === 'true' : false;
+ // switch is faster than a series of if/else
+ switch (oldType) {
+
+ case 'system':
+ badModes.push(item);
+ proxy.type = PROXY_TYPE_SYSTEM;
+ break;
+
+ case 'auto':
+ badModes.push(item);
+ if (item.getAttribute('autoconfMode') === 'pac') { // PAC
+ proxy.type = PROXY_TYPE_PAC;
+ proxy.pacURL = item.querySelector('autoconf').getAttribute('url');
+ }
+ else { // WPAD
+ proxy.type = PROXY_TYPE_WPAD;
+ proxy.pacURL = 'http://wpad/wpad.dat';
+ }
+ break;
+
+ case 'direct':
+ proxy.type = PROXY_TYPE_NONE;
+ break;
+
+ case 'manual':
+ const manualconf = item.querySelector('manualconf');
+ proxy.address = manualconf.getAttribute('host');
+ proxy.port = parseInt(manualconf.getAttribute('port'));
+ proxy.username = manualconf.getAttribute('username');
+ proxy.password = manualconf.getAttribute('password');
+ // There appears to be a bug in 4.6.5 and possibly earlier versions: socksversion is always 5, never 4
+ if (manualconf.getAttribute('isSocks') === 'true') {
+ proxy.type = PROXY_TYPE_SOCKS5;
+ if (item.getAttribute('proxyDNS') === 'true') { proxy.proxyDNS = true; }
+ }
+ else if (manualconf.getAttribute('isHttps') === 'true') { proxy.type = PROXY_TYPE_HTTPS; }
+ else { proxy.type = PROXY_TYPE_HTTP; }
+ break;
+ }
+
+ proxy.title = item.getAttribute('name');
+ proxy.color = item.getAttribute('color');
+
+ let newId;
+ const oldId = item.getAttribute('id');
+ if (item.getAttribute('lastresort') === 'true') {
+ lastResortFound = true;
+ newId = LASTRESORT; // this is a string
+ proxy.index = Number.MAX_SAFE_INTEGER;
+ if (!allowedType) { proxy.type = PROXY_TYPE_NONE; }
+ }
+ else {
+ proxy.index = index;
+ newId = 'import-' + oldId;
+ }
+
+ if (pref.mode === oldId) {
+ // If the old top-level mode points to a proxy setting with an unsupported mode (e.g. WPAD),
+ // we have to change the new top-level mode otherwise nothing will work w/o user intervention
+ pref.mode = !allowedType ? PROXY_TYPE_NONE : newId; // Update mode to the new id ("import-" prefix)
+ }
+ proxy.whitePatterns = [];
+ proxy.blackPatterns = [];
+
+ item.querySelectorAll('match').forEach(mtch => {
+
+ const newPattern = {};
+ /*
+ "whitePatterns": [
+ {
+ "title": "all URLs",
+ "active": true,
+ "pattern": "*",
+ "type": 1,
+ "protocols": 1
+ }
+ ]
+
+ */
+ newPattern.title = mtch.getAttribute('name');
+ newPattern.active = mtch.getAttribute('enabled') === 'true';
+ newPattern.importedPattern = newPattern.pattern = mtch.getAttribute('pattern');
+ newPattern.type = mtch.getAttribute('isRegEx') === 'true' ? PATTERN_TYPE_REGEXP : PATTERN_TYPE_WILDCARD;
+ // Do some simple parsing but only for wildcards. Anything else is going to fail.
+ if (newPattern.type === PATTERN_TYPE_WILDCARD) {
+
+ switch (true) {
+
+ case newPattern.pattern.startsWith('http://'):
+ newPattern.protocols = PROTOCOL_HTTP;
+ newPattern.pattern = newPattern.pattern.substring(7);
+ break;
+
+ case newPattern.pattern.startsWith('https://'):
+ newPattern.protocols = PROTOCOL_HTTPS;
+ newPattern.pattern = newPattern.pattern.substring(8);
+ break;
+
+ case newPattern.pattern.startsWith('*://'):
+ newPattern.protocols = PROTOCOL_ALL;
+ newPattern.pattern = newPattern.pattern.substring(4);
+ break;
+
+ default:
+ newPattern.protocols = PROTOCOL_ALL;
+ }
+
+ // Clip everything after slashes; it can't be used anymore: https://bugzilla.mozilla.org/show_bug.cgi?id=1337001
+ const idx = newPattern.pattern.indexOf('/');
+ if (idx > -1) {
+ newPattern.pattern = newPattern.pattern.substring(0, idx);
+ patternsEdited = true;
+ }
+ }
+ else { // e.g. ^https?://(?:[^:@/]+(?::[^@/]+)?@)?(?:localhost|127\.\d+\.\d+\.\d+)(?::\d+)?(?:/.*)?$
+
+ switch (true) {
+
+ case newPattern.pattern.indexOf('^http://') === 1:
+ newPattern.protocols = PROTOCOL_HTTP;
+ newPattern.pattern = '^' + newPattern.pattern.substring(8);
+ break;
+
+ case newPattern.pattern.indexOf('^https://') === 1:
+ newPattern.protocols = PROTOCOL_HTTPS;
+ newPattern.pattern = '^' + newPattern.pattern.substring(9);
+ break;
+
+ case newPattern.pattern.indexOf('^https?://') === 1:
+ newPattern.protocols = PROTOCOL_ALL;
+ newPattern.pattern = '^' + newPattern.pattern.substring(10);
+ break;
+
+ default:
+ newPattern.protocols = PROTOCOL_ALL;
+ }
+ }
+
+ mtch.getAttribute('isBlackList') === 'true' ? proxy.blackPatterns.push(newPattern) : proxy.whitePatterns.push(newPattern);
+ });
+
+ pref[newId] = proxy;
+ });
+
+ if (!lastResortFound) { pref[LASTRESORT] = DEFAULT_PROXY_SETTING; }
+
+ save(pref, () => endXML(patternsEdited));
+}
+
+function endXML(patternsEdited) {
+
+ hideSpinner();
+ if (patternsEdited) {
+ // Don't use Utils.notify() because at least on macOS,
+ // the message is too long and cut off
+ alert(chrome.i18n.getMessage('patternsChanged'));
+ location.href = '/options.html';
+ }
+ else {
+ // Don't use Utils.notify() because at least on macOS,
+ // the message is too long and cut off
+ alert(chrome.i18n.getMessage('importEndSlash'));
+ location.href = '/options.html';
+ }
+}
+
+
+function prepareForStorage(settings) {
+
+ if (!settings.hasOwnProperty('proxySettings') || !settings.proxySettings[0]) {
+ alert('Imported file doesn not have any proxies.');
+ return null;
+ }
+
+ // base format
+ const ret = {
+ mode: 'disabled',
+ logging: {
+ size: 100,
+ active: false
+ }
+ };
+
+ settings.mode && (ret.mode = settings.mode);
+ settings.logging && (ret.logging = settings.logging);
+
+ let idx = 0;
+ settings.proxySettings.forEach(item => {
+
+ const id = item.id;
+ item.index = idx++;
+ delete item.id; // Don't need id
+ ret[id] = item;
+ });
+
+ return ret;
+}
+
+
+// ----------------- FoxyProxy Import ----------------------
+function foxyProxyImport() {
+
+ // --- check user/pass
+ const username = document.querySelector('#username').value.trim();
+ const password = document.querySelector('#password').value.trim();
+ if (!username || !password) {
+ hideSpinner();
+ alert(chrome.i18n.getMessage('errorUserPass'));
+ return;
+ }
+
+ // --- generate the form post data
+ const usernamePassword = { 'username': username, 'password': password };
+ const formBody = [];
+ for (const property in usernamePassword) {
+ const encodedKey = encodeURIComponent(property);
+ const encodedValue = encodeURIComponent(usernamePassword[property]);
+ formBody.push(encodedKey + "=" + encodedValue);
+ }
+
+ // --- fetch data
+ fetch('https://getfoxyproxy.org/webservices/get-accounts.php',
+ { method: 'POST',
+ body: formBody.join("&"),
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
+ }
+ })
+ .then(response => response.json())
+ .then(response => {
+ if (!Array.isArray(response) || !response[0] || !response[0].hostname) {
+ hideSpinner();
+ Utils.notify(chrome.i18n.getMessage('errorFetch'));
+ return;
+ }
+
+ const sync = localStorage.getItem('sync') === 'true';
+ const storageArea = !sync ? chrome.storage.local : chrome.storage.sync;
+ storageArea.get(null, result => {
+
+ response.forEach(item => {
+ const hostname = item.hostname.substring(0, item.hostname.indexOf('.getfoxyproxy.org'));
+
+ if (hostname && item.ipaddress && item.port && item.port[0] && item.country_code && item.country) {
+
+ // --- creating proxy
+ result[Math.random().toString(36).substring(7) + new Date().getTime()] = {
+ index: -1,
+ active: item.active,
+ title: hostname,
+ color: '#ff9900',
+ type: 1, // HTTP
+ address: item.ipaddress,
+ port: item.port[0],
+ username: item.username,
+ password: item.password,
+ cc: item.country_code,
+ country: item.country,
+ whitePatterns: [],
+ blackPatterns: []
+ };
+ }
+ });
+
+ storageArea.set(result, end); // save to target
+ });
+ })
+ .catch(error => {
+ hideSpinner();
+ Utils.notify(chrome.i18n.getMessage('errorFetch'));
+ });
+
+}
+// ----------------- /FoxyProxy Import ---------------------
+
+function close() {
+ document.querySelector('#password').value = ''; /* prevent Firefox's save password prompt */
+ location.href = '/options.html';
+} \ No newline at end of file
diff --git a/src/scripts/jscolor-2.0.5.js b/src/scripts/jscolor-2.0.5.js
new file mode 100644
index 0000000..5c77177
--- /dev/null
+++ b/src/scripts/jscolor-2.0.5.js
@@ -0,0 +1,1855 @@
+/**
+ * jscolor - JavaScript Color Picker
+ *
+ * @link http://jscolor.com
+ * @license For open source use: GPLv3
+ * For commercial use: JSColor Commercial License
+ * @author Jan Odvarko
+ * @version 2.0.5
+ *
+ * See usage examples at http://jscolor.com/examples/
+ */
+
+
+"use strict";
+
+
+if (!window.jscolor) { window.jscolor = (function () {
+
+
+var jsc = {
+
+
+ register : function () {
+ jsc.attachDOMReadyEvent(jsc.init);
+ jsc.attachEvent(document, 'mousedown', jsc.onDocumentMouseDown);
+ jsc.attachEvent(document, 'touchstart', jsc.onDocumentTouchStart);
+ jsc.attachEvent(window, 'resize', jsc.onWindowResize);
+ },
+
+
+ init : function () {
+ if (jsc.jscolor.lookupClass) {
+ jsc.jscolor.installByClassName(jsc.jscolor.lookupClass);
+ }
+ },
+
+
+ tryInstallOnElements : function (elms, className) {
+ var matchClass = new RegExp('(^|\\s)(' + className + ')(\\s*(\\{[^}]*\\})|\\s|$)', 'i');
+
+ for (var i = 0; i < elms.length; i += 1) {
+ if (elms[i].type !== undefined && elms[i].type.toLowerCase() == 'color') {
+ if (jsc.isColorAttrSupported) {
+ // skip inputs of type 'color' if supported by the browser
+ continue;
+ }
+ }
+ var m;
+ if (!elms[i].jscolor && elms[i].className && (m = elms[i].className.match(matchClass))) {
+ var targetElm = elms[i];
+ var optsStr = null;
+
+ var dataOptions = jsc.getDataAttr(targetElm, 'jscolor');
+ if (dataOptions !== null) {
+ optsStr = dataOptions;
+ } else if (m[4]) {
+ optsStr = m[4];
+ }
+
+ var opts = {};
+ if (optsStr) {
+ try {
+ opts = (new Function ('return (' + optsStr + ')'))();
+ } catch(eParseError) {
+ jsc.warn('Error parsing jscolor options: ' + eParseError + ':\n' + optsStr);
+ }
+ }
+ targetElm.jscolor = new jsc.jscolor(targetElm, opts);
+ }
+ }
+ },
+
+
+ isColorAttrSupported : (function () {
+ var elm = document.createElement('input');
+ if (elm.setAttribute) {
+ elm.setAttribute('type', 'color');
+ if (elm.type.toLowerCase() == 'color') {
+ return true;
+ }
+ }
+ return false;
+ })(),
+
+
+ isCanvasSupported : (function () {
+ var elm = document.createElement('canvas');
+ return !!(elm.getContext && elm.getContext('2d'));
+ })(),
+
+
+ fetchElement : function (mixed) {
+ return typeof mixed === 'string' ? document.getElementById(mixed) : mixed;
+ },
+
+
+ isElementType : function (elm, type) {
+ return elm.nodeName.toLowerCase() === type.toLowerCase();
+ },
+
+
+ getDataAttr : function (el, name) {
+ var attrName = 'data-' + name;
+ var attrValue = el.getAttribute(attrName);
+ if (attrValue !== null) {
+ return attrValue;
+ }
+ return null;
+ },
+
+
+ attachEvent : function (el, evnt, func) {
+ if (el.addEventListener) {
+ el.addEventListener(evnt, func, false);
+ } else if (el.attachEvent) {
+ el.attachEvent('on' + evnt, func);
+ }
+ },
+
+
+ detachEvent : function (el, evnt, func) {
+ if (el.removeEventListener) {
+ el.removeEventListener(evnt, func, false);
+ } else if (el.detachEvent) {
+ el.detachEvent('on' + evnt, func);
+ }
+ },
+
+
+ _attachedGroupEvents : {},
+
+
+ attachGroupEvent : function (groupName, el, evnt, func) {
+ if (!jsc._attachedGroupEvents.hasOwnProperty(groupName)) {
+ jsc._attachedGroupEvents[groupName] = [];
+ }
+ jsc._attachedGroupEvents[groupName].push([el, evnt, func]);
+ jsc.attachEvent(el, evnt, func);
+ },
+
+
+ detachGroupEvents : function (groupName) {
+ if (jsc._attachedGroupEvents.hasOwnProperty(groupName)) {
+ for (var i = 0; i < jsc._attachedGroupEvents[groupName].length; i += 1) {
+ var evt = jsc._attachedGroupEvents[groupName][i];
+ jsc.detachEvent(evt[0], evt[1], evt[2]);
+ }
+ delete jsc._attachedGroupEvents[groupName];
+ }
+ },
+
+
+ attachDOMReadyEvent : function (func) {
+ var fired = false;
+ var fireOnce = function () {
+ if (!fired) {
+ fired = true;
+ func();
+ }
+ };
+
+ if (document.readyState === 'complete') {
+ setTimeout(fireOnce, 1); // async
+ return;
+ }
+
+ if (document.addEventListener) {
+ document.addEventListener('DOMContentLoaded', fireOnce, false);
+
+ // Fallback
+ window.addEventListener('load', fireOnce, false);
+
+ } else if (document.attachEvent) {
+ // IE
+ document.attachEvent('onreadystatechange', function () {
+ if (document.readyState === 'complete') {
+ document.detachEvent('onreadystatechange', arguments.callee);
+ fireOnce();
+ }
+ })
+
+ // Fallback
+ window.attachEvent('onload', fireOnce);
+
+ // IE7/8
+ if (document.documentElement.doScroll && window == window.top) {
+ var tryScroll = function () {
+ if (!document.body) { return; }
+ try {
+ document.documentElement.doScroll('left');
+ fireOnce();
+ } catch (e) {
+ setTimeout(tryScroll, 1);
+ }
+ };
+ tryScroll();
+ }
+ }
+ },
+
+
+ warn : function (msg) {
+ if (window.console && window.console.warn) {
+ window.console.warn(msg);
+ }
+ },
+
+
+ preventDefault : function (e) {
+ if (e.preventDefault) { e.preventDefault(); }
+ e.returnValue = false;
+ },
+
+
+ captureTarget : function (target) {
+ // IE
+ if (target.setCapture) {
+ jsc._capturedTarget = target;
+ jsc._capturedTarget.setCapture();
+ }
+ },
+
+
+ releaseTarget : function () {
+ // IE
+ if (jsc._capturedTarget) {
+ jsc._capturedTarget.releaseCapture();
+ jsc._capturedTarget = null;
+ }
+ },
+
+
+ fireEvent : function (el, evnt) {
+ if (!el) {
+ return;
+ }
+ if (document.createEvent) {
+ var ev = document.createEvent('HTMLEvents');
+ ev.initEvent(evnt, true, true);
+ el.dispatchEvent(ev);
+ } else if (document.createEventObject) {
+ var ev = document.createEventObject();
+ el.fireEvent('on' + evnt, ev);
+ } else if (el['on' + evnt]) { // alternatively use the traditional event model
+ el['on' + evnt]();
+ }
+ },
+
+
+ classNameToList : function (className) {
+ return className.replace(/^\s+|\s+$/g, '').split(/\s+/);
+ },
+
+
+ // The className parameter (str) can only contain a single class name
+ hasClass : function (elm, className) {
+ if (!className) {
+ return false;
+ }
+ return -1 != (' ' + elm.className.replace(/\s+/g, ' ') + ' ').indexOf(' ' + className + ' ');
+ },
+
+
+ // The className parameter (str) can contain multiple class names separated by whitespace
+ setClass : function (elm, className) {
+ var classList = jsc.classNameToList(className);
+ for (var i = 0; i < classList.length; i += 1) {
+ if (!jsc.hasClass(elm, classList[i])) {
+ elm.className += (elm.className ? ' ' : '') + classList[i];
+ }
+ }
+ },
+
+
+ // The className parameter (str) can contain multiple class names separated by whitespace
+ unsetClass : function (elm, className) {
+ var classList = jsc.classNameToList(className);
+ for (var i = 0; i < classList.length; i += 1) {
+ var repl = new RegExp(
+ '^\\s*' + classList[i] + '\\s*|' +
+ '\\s*' + classList[i] + '\\s*$|' +
+ '\\s+' + classList[i] + '(\\s+)',
+ 'g'
+ );
+ elm.className = elm.className.replace(repl, '$1');
+ }
+ },
+
+
+ getStyle : function (elm) {
+ return window.getComputedStyle ? window.getComputedStyle(elm) : elm.currentStyle;
+ },
+
+
+ setStyle : (function () {
+ var helper = document.createElement('div');
+ var getSupportedProp = function (names) {
+ for (var i = 0; i < names.length; i += 1) {
+ if (names[i] in helper.style) {
+ return names[i];
+ }
+ }
+ };
+ var props = {
+ borderRadius: getSupportedProp(['borderRadius', 'MozBorderRadius', 'webkitBorderRadius']),
+ boxShadow: getSupportedProp(['boxShadow', 'MozBoxShadow', 'webkitBoxShadow'])
+ };
+ return function (elm, prop, value) {
+ switch (prop.toLowerCase()) {
+ case 'opacity':
+ var alphaOpacity = Math.round(parseFloat(value) * 100);
+ elm.style.opacity = value;
+ elm.style.filter = 'alpha(opacity=' + alphaOpacity + ')';
+ break;
+ default:
+ elm.style[props[prop]] = value;
+ break;
+ }
+ };
+ })(),
+
+
+ setBorderRadius : function (elm, value) {
+ jsc.setStyle(elm, 'borderRadius', value || '0');
+ },
+
+
+ setBoxShadow : function (elm, value) {
+ jsc.setStyle(elm, 'boxShadow', value || 'none');
+ },
+
+
+ getElementPos : function (e, relativeToViewport) {
+ var x=0, y=0;
+ var rect = e.getBoundingClientRect();
+ x = rect.left;
+ y = rect.top;
+ if (!relativeToViewport) {
+ var viewPos = jsc.getViewPos();
+ x += viewPos[0];
+ y += viewPos[1];
+ }
+ return [x, y];
+ },
+
+
+ getElementSize : function (e) {
+ return [e.offsetWidth, e.offsetHeight];
+ },
+
+
+ // get pointer's X/Y coordinates relative to viewport
+ getAbsPointerPos : function (e) {
+ if (!e) { e = window.event; }
+ var x = 0, y = 0;
+ if (typeof e.changedTouches !== 'undefined' && e.changedTouches.length) {
+ // touch devices
+ x = e.changedTouches[0].clientX;
+ y = e.changedTouches[0].clientY;
+ } else if (typeof e.clientX === 'number') {
+ x = e.clientX;
+ y = e.clientY;
+ }
+ return { x: x, y: y };
+ },
+
+
+ // get pointer's X/Y coordinates relative to target element
+ getRelPointerPos : function (e) {
+ if (!e) { e = window.event; }
+ var target = e.target || e.srcElement;
+ var targetRect = target.getBoundingClientRect();
+
+ var x = 0, y = 0;
+
+ var clientX = 0, clientY = 0;
+ if (typeof e.changedTouches !== 'undefined' && e.changedTouches.length) {
+ // touch devices
+ clientX = e.changedTouches[0].clientX;
+ clientY = e.changedTouches[0].clientY;
+ } else if (typeof e.clientX === 'number') {
+ clientX = e.clientX;
+ clientY = e.clientY;
+ }
+
+ x = clientX - targetRect.left;
+ y = clientY - targetRect.top;
+ return { x: x, y: y };
+ },
+
+
+ getViewPos : function () {
+ var doc = document.documentElement;
+ return [
+ (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0),
+ (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0)
+ ];
+ },
+
+
+ getViewSize : function () {
+ var doc = document.documentElement;
+ return [
+ (window.innerWidth || doc.clientWidth),
+ (window.innerHeight || doc.clientHeight),
+ ];
+ },
+
+
+ redrawPosition : function () {
+
+ if (jsc.picker && jsc.picker.owner) {
+ var thisObj = jsc.picker.owner;
+
+ var tp, vp;
+
+ if (thisObj.fixed) {
+ // Fixed elements are positioned relative to viewport,
+ // therefore we can ignore the scroll offset
+ tp = jsc.getElementPos(thisObj.targetElement, true); // target pos
+ vp = [0, 0]; // view pos
+ } else {
+ tp = jsc.getElementPos(thisObj.targetElement); // target pos
+ vp = jsc.getViewPos(); // view pos
+ }
+
+ var ts = jsc.getElementSize(thisObj.targetElement); // target size
+ var vs = jsc.getViewSize(); // view size
+ var ps = jsc.getPickerOuterDims(thisObj); // picker size
+ var a, b, c;
+ switch (thisObj.position.toLowerCase()) {
+ case 'left': a=1; b=0; c=-1; break;
+ case 'right':a=1; b=0; c=1; break;
+ case 'top': a=0; b=1; c=-1; break;
+ default: a=0; b=1; c=1; break;
+ }
+ var l = (ts[b]+ps[b])/2;
+
+ // compute picker position
+ if (!thisObj.smartPosition) {
+ var pp = [
+ tp[a],
+ tp[b]+ts[b]-l+l*c
+ ];
+ } else {
+ var pp = [
+ -vp[a]+tp[a]+ps[a] > vs[a] ?
+ (-vp[a]+tp[a]+ts[a]/2 > vs[a]/2 && tp[a]+ts[a]-ps[a] >= 0 ? tp[a]+ts[a]-ps[a] : tp[a]) :
+ tp[a],
+ -vp[b]+tp[b]+ts[b]+ps[b]-l+l*c > vs[b] ?
+ (-vp[b]+tp[b]+ts[b]/2 > vs[b]/2 && tp[b]+ts[b]-l-l*c >= 0 ? tp[b]+ts[b]-l-l*c : tp[b]+ts[b]-l+l*c) :
+ (tp[b]+ts[b]-l+l*c >= 0 ? tp[b]+ts[b]-l+l*c : tp[b]+ts[b]-l-l*c)
+ ];
+ }
+
+ var x = pp[a];
+ var y = pp[b];
+ var positionValue = thisObj.fixed ? 'fixed' : 'absolute';
+ var contractShadow =
+ (pp[0] + ps[0] > tp[0] || pp[0] < tp[0] + ts[0]) &&
+ (pp[1] + ps[1] < tp[1] + ts[1]);
+
+ jsc._drawPosition(thisObj, x, y, positionValue, contractShadow);
+ }
+ },
+
+
+ _drawPosition : function (thisObj, x, y, positionValue, contractShadow) {
+ var vShadow = contractShadow ? 0 : thisObj.shadowBlur; // px
+
+ jsc.picker.wrap.style.position = positionValue;
+ jsc.picker.wrap.style.left = x + 'px';
+ jsc.picker.wrap.style.top = y + 'px';
+
+ jsc.setBoxShadow(
+ jsc.picker.boxS,
+ thisObj.shadow ?
+ new jsc.BoxShadow(0, vShadow, thisObj.shadowBlur, 0, thisObj.shadowColor) :
+ null);
+ },
+
+
+ getPickerDims : function (thisObj) {
+ var displaySlider = !!jsc.getSliderComponent(thisObj);
+ var dims = [
+ 2 * thisObj.insetWidth + 2 * thisObj.padding + thisObj.width +
+ (displaySlider ? 2 * thisObj.insetWidth + jsc.getPadToSliderPadding(thisObj) + thisObj.sliderSize : 0),
+ 2 * thisObj.insetWidth + 2 * thisObj.padding + thisObj.height +
+ (thisObj.closable ? 2 * thisObj.insetWidth + thisObj.padding + thisObj.buttonHeight : 0)
+ ];
+ return dims;
+ },
+
+
+ getPickerOuterDims : function (thisObj) {
+ var dims = jsc.getPickerDims(thisObj);
+ return [
+ dims[0] + 2 * thisObj.borderWidth,
+ dims[1] + 2 * thisObj.borderWidth
+ ];
+ },
+
+
+ getPadToSliderPadding : function (thisObj) {
+ return Math.max(thisObj.padding, 1.5 * (2 * thisObj.pointerBorderWidth + thisObj.pointerThickness));
+ },
+
+
+ getPadYComponent : function (thisObj) {
+ switch (thisObj.mode.charAt(1).toLowerCase()) {
+ case 'v': return 'v'; break;
+ }
+ return 's';
+ },
+
+
+ getSliderComponent : function (thisObj) {
+ if (thisObj.mode.length > 2) {
+ switch (thisObj.mode.charAt(2).toLowerCase()) {
+ case 's': return 's'; break;
+ case 'v': return 'v'; break;
+ }
+ }
+ return null;
+ },
+
+
+ onDocumentMouseDown : function (e) {
+ if (!e) { e = window.event; }
+ var target = e.target || e.srcElement;
+
+ if (target._jscLinkedInstance) {
+ if (target._jscLinkedInstance.showOnClick) {
+ target._jscLinkedInstance.show();
+ }
+ } else if (target._jscControlName) {
+ jsc.onControlPointerStart(e, target, target._jscControlName, 'mouse');
+ } else {
+ // Mouse is outside the picker controls -> hide the color picker!
+ if (jsc.picker && jsc.picker.owner) {
+ jsc.picker.owner.hide();
+ }
+ }
+ },
+
+
+ onDocumentTouchStart : function (e) {
+ if (!e) { e = window.event; }
+ var target = e.target || e.srcElement;
+
+ if (target._jscLinkedInstance) {
+ if (target._jscLinkedInstance.showOnClick) {
+ target._jscLinkedInstance.show();
+ }
+ } else if (target._jscControlName) {
+ jsc.onControlPointerStart(e, target, target._jscControlName, 'touch');
+ } else {
+ if (jsc.picker && jsc.picker.owner) {
+ jsc.picker.owner.hide();
+ }
+ }
+ },
+
+
+ onWindowResize : function (e) {
+ jsc.redrawPosition();
+ },
+
+
+ onParentScroll : function (e) {
+ // hide the picker when one of the parent elements is scrolled
+ if (jsc.picker && jsc.picker.owner) {
+ jsc.picker.owner.hide();
+ }
+ },
+
+
+ _pointerMoveEvent : {
+ mouse: 'mousemove',
+ touch: 'touchmove'
+ },
+ _pointerEndEvent : {
+ mouse: 'mouseup',
+ touch: 'touchend'
+ },
+
+
+ _pointerOrigin : null,
+ _capturedTarget : null,
+
+
+ onControlPointerStart : function (e, target, controlName, pointerType) {
+ var thisObj = target._jscInstance;
+
+ jsc.preventDefault(e);
+ jsc.captureTarget(target);
+
+ var registerDragEvents = function (doc, offset) {
+ jsc.attachGroupEvent('drag', doc, jsc._pointerMoveEvent[pointerType],
+ jsc.onDocumentPointerMove(e, target, controlName, pointerType, offset));
+ jsc.attachGroupEvent('drag', doc, jsc._pointerEndEvent[pointerType],
+ jsc.onDocumentPointerEnd(e, target, controlName, pointerType));
+ };
+
+ registerDragEvents(document, [0, 0]);
+
+ if (window.parent && window.frameElement) {
+ var rect = window.frameElement.getBoundingClientRect();
+ var ofs = [-rect.left, -rect.top];
+ registerDragEvents(window.parent.window.document, ofs);
+ }
+
+ var abs = jsc.getAbsPointerPos(e);
+ var rel = jsc.getRelPointerPos(e);
+ jsc._pointerOrigin = {
+ x: abs.x - rel.x,
+ y: abs.y - rel.y
+ };
+
+ switch (controlName) {
+ case 'pad':
+ // if the slider is at the bottom, move it up
+ switch (jsc.getSliderComponent(thisObj)) {
+ case 's': if (thisObj.hsv[1] === 0) { thisObj.fromHSV(null, 100, null); }; break;
+ case 'v': if (thisObj.hsv[2] === 0) { thisObj.fromHSV(null, null, 100); }; break;
+ }
+ jsc.setPad(thisObj, e, 0, 0);
+ break;
+
+ case 'sld':
+ jsc.setSld(thisObj, e, 0);
+ break;
+ }
+
+ jsc.dispatchFineChange(thisObj);
+ },
+
+
+ onDocumentPointerMove : function (e, target, controlName, pointerType, offset) {
+ return function (e) {
+ var thisObj = target._jscInstance;
+ switch (controlName) {
+ case 'pad':
+ if (!e) { e = window.event; }
+ jsc.setPad(thisObj, e, offset[0], offset[1]);
+ jsc.dispatchFineChange(thisObj);
+ break;
+
+ case 'sld':
+ if (!e) { e = window.event; }
+ jsc.setSld(thisObj, e, offset[1]);
+ jsc.dispatchFineChange(thisObj);
+ break;
+ }
+ }
+ },
+
+
+ onDocumentPointerEnd : function (e, target, controlName, pointerType) {
+ return function (e) {
+ var thisObj = target._jscInstance;
+ jsc.detachGroupEvents('drag');
+ jsc.releaseTarget();
+ // Always dispatch changes after detaching outstanding mouse handlers,
+ // in case some user interaction will occur in user's onchange callback
+ // that would intrude with current mouse events
+ jsc.dispatchChange(thisObj);
+ };
+ },
+
+
+ dispatchChange : function (thisObj) {
+ if (thisObj.valueElement) {
+ if (jsc.isElementType(thisObj.valueElement, 'input')) {
+ jsc.fireEvent(thisObj.valueElement, 'change');
+ }
+ }
+ },
+
+
+ dispatchFineChange : function (thisObj) {
+ if (thisObj.onFineChange) {
+ var callback;
+ if (typeof thisObj.onFineChange === 'string') {
+ callback = new Function (thisObj.onFineChange);
+ } else {
+ callback = thisObj.onFineChange;
+ }
+ callback.call(thisObj);
+ }
+ },
+
+
+ setPad : function (thisObj, e, ofsX, ofsY) {
+ var pointerAbs = jsc.getAbsPointerPos(e);
+ var x = ofsX + pointerAbs.x - jsc._pointerOrigin.x - thisObj.padding - thisObj.insetWidth;
+ var y = ofsY + pointerAbs.y - jsc._pointerOrigin.y - thisObj.padding - thisObj.insetWidth;
+
+ var xVal = x * (360 / (thisObj.width - 1));
+ var yVal = 100 - (y * (100 / (thisObj.height - 1)));
+
+ switch (jsc.getPadYComponent(thisObj)) {
+ case 's': thisObj.fromHSV(xVal, yVal, null, jsc.leaveSld); break;
+ case 'v': thisObj.fromHSV(xVal, null, yVal, jsc.leaveSld); break;
+ }
+ },
+
+
+ setSld : function (thisObj, e, ofsY) {
+ var pointerAbs = jsc.getAbsPointerPos(e);
+ var y = ofsY + pointerAbs.y - jsc._pointerOrigin.y - thisObj.padding - thisObj.insetWidth;
+
+ var yVal = 100 - (y * (100 / (thisObj.height - 1)));
+
+ switch (jsc.getSliderComponent(thisObj)) {
+ case 's': thisObj.fromHSV(null, yVal, null, jsc.leavePad); break;
+ case 'v': thisObj.fromHSV(null, null, yVal, jsc.leavePad); break;
+ }
+ },
+
+
+ _vmlNS : 'jsc_vml_',
+ _vmlCSS : 'jsc_vml_css_',
+ _vmlReady : false,
+
+
+ initVML : function () {
+ if (!jsc._vmlReady) {
+ // init VML namespace
+ var doc = document;
+ if (!doc.namespaces[jsc._vmlNS]) {
+ doc.namespaces.add(jsc._vmlNS, 'urn:schemas-microsoft-com:vml');
+ }
+ if (!doc.styleSheets[jsc._vmlCSS]) {
+ var tags = ['shape', 'shapetype', 'group', 'background', 'path', 'formulas', 'handles', 'fill', 'stroke', 'shadow', 'textbox', 'textpath', 'imagedata', 'line', 'polyline', 'curve', 'rect', 'roundrect', 'oval', 'arc', 'image'];
+ var ss = doc.createStyleSheet();
+ ss.owningElement.id = jsc._vmlCSS;
+ for (var i = 0; i < tags.length; i += 1) {
+ ss.addRule(jsc._vmlNS + '\\:' + tags[i], 'behavior:url(#default#VML);');
+ }
+ }
+ jsc._vmlReady = true;
+ }
+ },
+
+
+ createPalette : function () {
+
+ var paletteObj = {
+ elm: null,
+ draw: null
+ };
+
+ if (jsc.isCanvasSupported) {
+ // Canvas implementation for modern browsers
+
+ var canvas = document.createElement('canvas');
+ var ctx = canvas.getContext('2d');
+
+ var drawFunc = function (width, height, type) {
+ canvas.width = width;
+ canvas.height = height;
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ var hGrad = ctx.createLinearGradient(0, 0, canvas.width, 0);
+ hGrad.addColorStop(0 / 6, '#F00');
+ hGrad.addColorStop(1 / 6, '#FF0');
+ hGrad.addColorStop(2 / 6, '#0F0');
+ hGrad.addColorStop(3 / 6, '#0FF');
+ hGrad.addColorStop(4 / 6, '#00F');
+ hGrad.addColorStop(5 / 6, '#F0F');
+ hGrad.addColorStop(6 / 6, '#F00');
+
+ ctx.fillStyle = hGrad;
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+
+ var vGrad = ctx.createLinearGradient(0, 0, 0, canvas.height);
+ switch (type.toLowerCase()) {
+ case 's':
+ vGrad.addColorStop(0, 'rgba(255,255,255,0)');
+ vGrad.addColorStop(1, 'rgba(255,255,255,1)');
+ break;
+ case 'v':
+ vGrad.addColorStop(0, 'rgba(0,0,0,0)');
+ vGrad.addColorStop(1, 'rgba(0,0,0,1)');
+ break;
+ }
+ ctx.fillStyle = vGrad;
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ };
+
+ paletteObj.elm = canvas;
+ paletteObj.draw = drawFunc;
+
+ } else {
+ // VML fallback for IE 7 and 8
+
+ jsc.initVML();
+
+ var vmlContainer = document.createElement('div');
+ vmlContainer.style.position = 'relative';
+ vmlContainer.style.overflow = 'hidden';
+
+ var hGrad = document.createElement(jsc._vmlNS + ':fill');
+ hGrad.type = 'gradient';
+ hGrad.method = 'linear';
+ hGrad.angle = '90';
+ hGrad.colors = '16.67% #F0F, 33.33% #00F, 50% #0FF, 66.67% #0F0, 83.33% #FF0'
+
+ var hRect = document.createElement(jsc._vmlNS + ':rect');
+ hRect.style.position = 'absolute';
+ hRect.style.left = -1 + 'px';
+ hRect.style.top = -1 + 'px';
+ hRect.stroked = false;
+ hRect.appendChild(hGrad);
+ vmlContainer.appendChild(hRect);
+
+ var vGrad = document.createElement(jsc._vmlNS + ':fill');
+ vGrad.type = 'gradient';
+ vGrad.method = 'linear';
+ vGrad.angle = '180';
+ vGrad.opacity = '0';
+
+ var vRect = document.createElement(jsc._vmlNS + ':rect');
+ vRect.style.position = 'absolute';
+ vRect.style.left = -1 + 'px';
+ vRect.style.top = -1 + 'px';
+ vRect.stroked = false;
+ vRect.appendChild(vGrad);
+ vmlContainer.appendChild(vRect);
+
+ var drawFunc = function (width, height, type) {
+ vmlContainer.style.width = width + 'px';
+ vmlContainer.style.height = height + 'px';
+
+ hRect.style.width =
+ vRect.style.width =
+ (width + 1) + 'px';
+ hRect.style.height =
+ vRect.style.height =
+ (height + 1) + 'px';
+
+ // Colors must be specified during every redraw, otherwise IE won't display
+ // a full gradient during a subsequential redraw
+ hGrad.color = '#F00';
+ hGrad.color2 = '#F00';
+
+ switch (type.toLowerCase()) {
+ case 's':
+ vGrad.color = vGrad.color2 = '#FFF';
+ break;
+ case 'v':
+ vGrad.color = vGrad.color2 = '#000';
+ break;
+ }
+ };
+
+ paletteObj.elm = vmlContainer;
+ paletteObj.draw = drawFunc;
+ }
+
+ return paletteObj;
+ },
+
+
+ createSliderGradient : function () {
+
+ var sliderObj = {
+ elm: null,
+ draw: null
+ };
+
+ if (jsc.isCanvasSupported) {
+ // Canvas implementation for modern browsers
+
+ var canvas = document.createElement('canvas');
+ var ctx = canvas.getContext('2d');
+
+ var drawFunc = function (width, height, color1, color2) {
+ canvas.width = width;
+ canvas.height = height;
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ var grad = ctx.createLinearGradient(0, 0, 0, canvas.height);
+ grad.addColorStop(0, color1);
+ grad.addColorStop(1, color2);
+
+ ctx.fillStyle = grad;
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ };
+
+ sliderObj.elm = canvas;
+ sliderObj.draw = drawFunc;
+
+ } else {
+ // VML fallback for IE 7 and 8
+
+ jsc.initVML();
+
+ var vmlContainer = document.createElement('div');
+ vmlContainer.style.position = 'relative';
+ vmlContainer.style.overflow = 'hidden';
+
+ var grad = document.createElement(jsc._vmlNS + ':fill');
+ grad.type = 'gradient';
+ grad.method = 'linear';
+ grad.angle = '180';
+
+ var rect = document.createElement(jsc._vmlNS + ':rect');
+ rect.style.position = 'absolute';
+ rect.style.left = -1 + 'px';
+ rect.style.top = -1 + 'px';
+ rect.stroked = false;
+ rect.appendChild(grad);
+ vmlContainer.appendChild(rect);
+
+ var drawFunc = function (width, height, color1, color2) {
+ vmlContainer.style.width = width + 'px';
+ vmlContainer.style.height = height + 'px';
+
+ rect.style.width = (width + 1) + 'px';
+ rect.style.height = (height + 1) + 'px';
+
+ grad.color = color1;
+ grad.color2 = color2;
+ };
+
+ sliderObj.elm = vmlContainer;
+ sliderObj.draw = drawFunc;
+ }
+
+ return sliderObj;
+ },
+
+
+ leaveValue : 1<<0,
+ leaveStyle : 1<<1,
+ leavePad : 1<<2,
+ leaveSld : 1<<3,
+
+
+ BoxShadow : (function () {
+ var BoxShadow = function (hShadow, vShadow, blur, spread, color, inset) {
+ this.hShadow = hShadow;
+ this.vShadow = vShadow;
+ this.blur = blur;
+ this.spread = spread;
+ this.color = color;
+ this.inset = !!inset;
+ };
+
+ BoxShadow.prototype.toString = function () {
+ var vals = [
+ Math.round(this.hShadow) + 'px',
+ Math.round(this.vShadow) + 'px',
+ Math.round(this.blur) + 'px',
+ Math.round(this.spread) + 'px',
+ this.color
+ ];
+ if (this.inset) {
+ vals.push('inset');
+ }
+ return vals.join(' ');
+ };
+
+ return BoxShadow;
+ })(),
+
+
+ //
+ // Usage:
+ // var myColor = new jscolor(<targetElement> [, <options>])
+ //
+
+ jscolor : function (targetElement, options) {
+
+ // General options
+ //
+ this.value = null; // initial HEX color. To change it later, use methods fromString(), fromHSV() and fromRGB()
+ this.valueElement = targetElement; // element that will be used to display and input the color code
+ this.styleElement = targetElement; // element that will preview the picked color using CSS backgroundColor
+ this.required = true; // whether the associated text <input> can be left empty
+ this.refine = true; // whether to refine the entered color code (e.g. uppercase it and remove whitespace)
+ this.hash = false; // whether to prefix the HEX color code with # symbol
+ this.uppercase = true; // whether to show the color code in upper case
+ this.onFineChange = null; // called instantly every time the color changes (value can be either a function or a string with javascript code)
+ this.activeClass = 'jscolor-active'; // class to be set to the target element when a picker window is open on it
+ this.overwriteImportant = false; // whether to overwrite colors of styleElement using !important
+ this.minS = 0; // min allowed saturation (0 - 100)
+ this.maxS = 100; // max allowed saturation (0 - 100)
+ this.minV = 0; // min allowed value (brightness) (0 - 100)
+ this.maxV = 100; // max allowed value (brightness) (0 - 100)
+
+ // Accessing the picked color
+ //
+ this.hsv = [0, 0, 100]; // read-only [0-360, 0-100, 0-100]
+ this.rgb = [255, 255, 255]; // read-only [0-255, 0-255, 0-255]
+
+ // Color Picker options
+ //
+ this.width = 181; // width of color palette (in px)
+ this.height = 101; // height of color palette (in px)
+ this.showOnClick = true; // whether to display the color picker when user clicks on its target element
+ this.mode = 'HSV'; // HSV | HVS | HS | HV - layout of the color picker controls
+ this.position = 'bottom'; // left | right | top | bottom - position relative to the target element
+ this.smartPosition = true; // automatically change picker position when there is not enough space for it
+ this.sliderSize = 16; // px
+ this.crossSize = 8; // px
+ this.closable = false; // whether to display the Close button
+ this.closeText = 'Close';
+ this.buttonColor = '#000000'; // CSS color
+ this.buttonHeight = 18; // px
+ this.padding = 12; // px
+ this.backgroundColor = '#FFFFFF'; // CSS color
+ this.borderWidth = 1; // px
+ this.borderColor = '#BBBBBB'; // CSS color
+ this.borderRadius = 8; // px
+ this.insetWidth = 1; // px
+ this.insetColor = '#BBBBBB'; // CSS color
+ this.shadow = true; // whether to display shadow
+ this.shadowBlur = 15; // px
+ this.shadowColor = 'rgba(0,0,0,0.2)'; // CSS color
+ this.pointerColor = '#4C4C4C'; // px
+ this.pointerBorderColor = '#FFFFFF'; // px
+ this.pointerBorderWidth = 1; // px
+ this.pointerThickness = 2; // px
+ this.zIndex = 1000;
+ this.container = null; // where to append the color picker (BODY element by default)
+
+
+ for (var opt in options) {
+ if (options.hasOwnProperty(opt)) {
+ this[opt] = options[opt];
+ }
+ }
+
+
+ this.hide = function () {
+ if (isPickerOwner()) {
+ detachPicker();
+ }
+ };
+
+
+ this.show = function () {
+ drawPicker();
+ };
+
+
+ this.redraw = function () {
+ if (isPickerOwner()) {
+ drawPicker();
+ }
+ };
+
+
+ this.importColor = function () {
+ if (!this.valueElement) {
+ this.exportColor();
+ } else {
+ if (jsc.isElementType(this.valueElement, 'input')) {
+ if (!this.refine) {
+ if (!this.fromString(this.valueElement.value, jsc.leaveValue)) {
+ if (this.styleElement) {
+ this.styleElement.style.backgroundImage = this.styleElement._jscOrigStyle.backgroundImage;
+ this.styleElement.style.backgroundColor = this.styleElement._jscOrigStyle.backgroundColor;
+ this.styleElement.style.color = this.styleElement._jscOrigStyle.color;
+ }
+ this.exportColor(jsc.leaveValue | jsc.leaveStyle);
+ }
+ } else if (!this.required && /^\s*$/.test(this.valueElement.value)) {
+ this.valueElement.value = '';
+ if (this.styleElement) {
+ this.styleElement.style.backgroundImage = this.styleElement._jscOrigStyle.backgroundImage;
+ this.styleElement.style.backgroundColor = this.styleElement._jscOrigStyle.backgroundColor;
+ this.styleElement.style.color = this.styleElement._jscOrigStyle.color;
+ }
+ this.exportColor(jsc.leaveValue | jsc.leaveStyle);
+
+ } else if (this.fromString(this.valueElement.value)) {
+ // managed to import color successfully from the value -> OK, don't do anything
+ } else {
+ this.exportColor();
+ }
+ } else {
+ // not an input element -> doesn't have any value
+ this.exportColor();
+ }
+ }
+ };
+
+
+ this.exportColor = function (flags) {
+ if (!(flags & jsc.leaveValue) && this.valueElement) {
+ var value = this.toString();
+ if (this.uppercase) { value = value.toUpperCase(); }
+ if (this.hash) { value = '#' + value; }
+
+ if (jsc.isElementType(this.valueElement, 'input')) {
+ this.valueElement.value = value;
+ } else {
+ this.valueElement.innerHTML = value;
+ }
+ }
+ if (!(flags & jsc.leaveStyle)) {
+ if (this.styleElement) {
+ var bgColor = '#' + this.toString();
+ var fgColor = this.isLight() ? '#000' : '#FFF';
+
+ this.styleElement.style.backgroundImage = 'none';
+ this.styleElement.style.backgroundColor = bgColor;
+ this.styleElement.style.color = fgColor;
+
+ if (this.overwriteImportant) {
+ this.styleElement.setAttribute('style',
+ 'background: ' + bgColor + ' !important; ' +
+ 'color: ' + fgColor + ' !important;'
+ );
+ }
+ }
+ }
+ if (!(flags & jsc.leavePad) && isPickerOwner()) {
+ redrawPad();
+ }
+ if (!(flags & jsc.leaveSld) && isPickerOwner()) {
+ redrawSld();
+ }
+ };
+
+
+ // h: 0-360
+ // s: 0-100
+ // v: 0-100
+ //
+ this.fromHSV = function (h, s, v, flags) { // null = don't change
+ if (h !== null) {
+ if (isNaN(h)) { return false; }
+ h = Math.max(0, Math.min(360, h));
+ }
+ if (s !== null) {
+ if (isNaN(s)) { return false; }
+ s = Math.max(0, Math.min(100, this.maxS, s), this.minS);
+ }
+ if (v !== null) {
+ if (isNaN(v)) { return false; }
+ v = Math.max(0, Math.min(100, this.maxV, v), this.minV);
+ }
+
+ this.rgb = HSV_RGB(
+ h===null ? this.hsv[0] : (this.hsv[0]=h),
+ s===null ? this.hsv[1] : (this.hsv[1]=s),
+ v===null ? this.hsv[2] : (this.hsv[2]=v)
+ );
+
+ this.exportColor(flags);
+ };
+
+
+ // r: 0-255
+ // g: 0-255
+ // b: 0-255
+ //
+ this.fromRGB = function (r, g, b, flags) { // null = don't change
+ if (r !== null) {
+ if (isNaN(r)) { return false; }
+ r = Math.max(0, Math.min(255, r));
+ }
+ if (g !== null) {
+ if (isNaN(g)) { return false; }
+ g = Math.max(0, Math.min(255, g));
+ }
+ if (b !== null) {
+ if (isNaN(b)) { return false; }
+ b = Math.max(0, Math.min(255, b));
+ }
+
+ var hsv = RGB_HSV(
+ r===null ? this.rgb[0] : r,
+ g===null ? this.rgb[1] : g,
+ b===null ? this.rgb[2] : b
+ );
+ if (hsv[0] !== null) {
+ this.hsv[0] = Math.max(0, Math.min(360, hsv[0]));
+ }
+ if (hsv[2] !== 0) {
+ this.hsv[1] = hsv[1]===null ? null : Math.max(0, this.minS, Math.min(100, this.maxS, hsv[1]));
+ }
+ this.hsv[2] = hsv[2]===null ? null : Math.max(0, this.minV, Math.min(100, this.maxV, hsv[2]));
+
+ // update RGB according to final HSV, as some values might be trimmed
+ var rgb = HSV_RGB(this.hsv[0], this.hsv[1], this.hsv[2]);
+ this.rgb[0] = rgb[0];
+ this.rgb[1] = rgb[1];
+ this.rgb[2] = rgb[2];
+
+ this.exportColor(flags);
+ };
+
+
+ this.fromString = function (str, flags) {
+ var m;
+ if (m = str.match(/^\W*([0-9A-F]{3}([0-9A-F]{3})?)\W*$/i)) {
+ // HEX notation
+ //
+
+ if (m[1].length === 6) {
+ // 6-char notation
+ this.fromRGB(
+ parseInt(m[1].substr(0,2),16),
+ parseInt(m[1].substr(2,2),16),
+ parseInt(m[1].substr(4,2),16),
+ flags
+ );
+ } else {
+ // 3-char notation
+ this.fromRGB(
+ parseInt(m[1].charAt(0) + m[1].charAt(0),16),
+ parseInt(m[1].charAt(1) + m[1].charAt(1),16),
+ parseInt(m[1].charAt(2) + m[1].charAt(2),16),
+ flags
+ );
+ }
+ return true;
+
+ } else if (m = str.match(/^\W*rgba?\(([^)]*)\)\W*$/i)) {
+ var params = m[1].split(',');
+ var re = /^\s*(\d*)(\.\d+)?\s*$/;
+ var mR, mG, mB;
+ if (
+ params.length >= 3 &&
+ (mR = params[0].match(re)) &&
+ (mG = params[1].match(re)) &&
+ (mB = params[2].match(re))
+ ) {
+ var r = parseFloat((mR[1] || '0') + (mR[2] || ''));
+ var g = parseFloat((mG[1] || '0') + (mG[2] || ''));
+ var b = parseFloat((mB[1] || '0') + (mB[2] || ''));
+ this.fromRGB(r, g, b, flags);
+ return true;
+ }
+ }
+ return false;
+ };
+
+
+ this.toString = function () {
+ return (
+ (0x100 | Math.round(this.rgb[0])).toString(16).substr(1) +
+ (0x100 | Math.round(this.rgb[1])).toString(16).substr(1) +
+ (0x100 | Math.round(this.rgb[2])).toString(16).substr(1)
+ );
+ };
+
+
+ this.toHEXString = function () {
+ return '#' + this.toString().toUpperCase();
+ };
+
+
+ this.toRGBString = function () {
+ return ('rgb(' +
+ Math.round(this.rgb[0]) + ',' +
+ Math.round(this.rgb[1]) + ',' +
+ Math.round(this.rgb[2]) + ')'
+ );
+ };
+
+
+ this.isLight = function () {
+ return (
+ 0.213 * this.rgb[0] +
+ 0.715 * this.rgb[1] +
+ 0.072 * this.rgb[2] >
+ 255 / 2
+ );
+ };
+
+
+ this._processParentElementsInDOM = function () {
+ if (this._linkedElementsProcessed) { return; }
+ this._linkedElementsProcessed = true;
+
+ var elm = this.targetElement;
+ do {
+ // If the target element or one of its parent nodes has fixed position,
+ // then use fixed positioning instead
+ //
+ // Note: In Firefox, getComputedStyle returns null in a hidden iframe,
+ // that's why we need to check if the returned style object is non-empty
+ var currStyle = jsc.getStyle(elm);
+ if (currStyle && currStyle.position.toLowerCase() === 'fixed') {
+ this.fixed = true;
+ }
+
+ if (elm !== this.targetElement) {
+ // Ensure to attach onParentScroll only once to each parent element
+ // (multiple targetElements can share the same parent nodes)
+ //
+ // Note: It's not just offsetParents that can be scrollable,
+ // that's why we loop through all parent nodes
+ if (!elm._jscEventsAttached) {
+ jsc.attachEvent(elm, 'scroll', jsc.onParentScroll);
+ elm._jscEventsAttached = true;
+ }
+ }
+ } while ((elm = elm.parentNode) && !jsc.isElementType(elm, 'body'));
+ };
+
+
+ // r: 0-255
+ // g: 0-255
+ // b: 0-255
+ //
+ // returns: [ 0-360, 0-100, 0-100 ]
+ //
+ function RGB_HSV (r, g, b) {
+ r /= 255;
+ g /= 255;
+ b /= 255;
+ var n = Math.min(Math.min(r,g),b);
+ var v = Math.max(Math.max(r,g),b);
+ var m = v - n;
+ if (m === 0) { return [ null, 0, 100 * v ]; }
+ var h = r===n ? 3+(b-g)/m : (g===n ? 5+(r-b)/m : 1+(g-r)/m);
+ return [
+ 60 * (h===6?0:h),
+ 100 * (m/v),
+ 100 * v
+ ];
+ }
+
+
+ // h: 0-360
+ // s: 0-100
+ // v: 0-100
+ //
+ // returns: [ 0-255, 0-255, 0-255 ]
+ //
+ function HSV_RGB (h, s, v) {
+ var u = 255 * (v / 100);
+
+ if (h === null) {
+ return [ u, u, u ];
+ }
+
+ h /= 60;
+ s /= 100;
+
+ var i = Math.floor(h);
+ var f = i%2 ? h-i : 1-(h-i);
+ var m = u * (1 - s);
+ var n = u * (1 - s * f);
+ switch (i) {
+ case 6:
+ case 0: return [u,n,m];
+ case 1: return [n,u,m];
+ case 2: return [m,u,n];
+ case 3: return [m,n,u];
+ case 4: return [n,m,u];
+ case 5: return [u,m,n];
+ }
+ }
+
+
+ function detachPicker () {
+ jsc.unsetClass(THIS.targetElement, THIS.activeClass);
+ jsc.picker.wrap.parentNode.removeChild(jsc.picker.wrap);
+ delete jsc.picker.owner;
+ }
+
+
+ function drawPicker () {
+
+ // At this point, when drawing the picker, we know what the parent elements are
+ // and we can do all related DOM operations, such as registering events on them
+ // or checking their positioning
+ THIS._processParentElementsInDOM();
+
+ if (!jsc.picker) {
+ jsc.picker = {
+ owner: null,
+ wrap : document.createElement('div'),
+ box : document.createElement('div'),
+ boxS : document.createElement('div'), // shadow area
+ boxB : document.createElement('div'), // border
+ pad : document.createElement('div'),
+ padB : document.createElement('div'), // border
+ padM : document.createElement('div'), // mouse/touch area
+ padPal : jsc.createPalette(),
+ cross : document.createElement('div'),
+ crossBY : document.createElement('div'), // border Y
+ crossBX : document.createElement('div'), // border X
+ crossLY : document.createElement('div'), // line Y
+ crossLX : document.createElement('div'), // line X
+ sld : document.createElement('div'),
+ sldB : document.createElement('div'), // border
+ sldM : document.createElement('div'), // mouse/touch area
+ sldGrad : jsc.createSliderGradient(),
+ sldPtrS : document.createElement('div'), // slider pointer spacer
+ sldPtrIB : document.createElement('div'), // slider pointer inner border
+ sldPtrMB : document.createElement('div'), // slider pointer middle border
+ sldPtrOB : document.createElement('div'), // slider pointer outer border
+ btn : document.createElement('div'),
+ btnT : document.createElement('span') // text
+ };
+
+ jsc.picker.pad.appendChild(jsc.picker.padPal.elm);
+ jsc.picker.padB.appendChild(jsc.picker.pad);
+ jsc.picker.cross.appendChild(jsc.picker.crossBY);
+ jsc.picker.cross.appendChild(jsc.picker.crossBX);
+ jsc.picker.cross.appendChild(jsc.picker.crossLY);
+ jsc.picker.cross.appendChild(jsc.picker.crossLX);
+ jsc.picker.padB.appendChild(jsc.picker.cross);
+ jsc.picker.box.appendChild(jsc.picker.padB);
+ jsc.picker.box.appendChild(jsc.picker.padM);
+
+ jsc.picker.sld.appendChild(jsc.picker.sldGrad.elm);
+ jsc.picker.sldB.appendChild(jsc.picker.sld);
+ jsc.picker.sldB.appendChild(jsc.picker.sldPtrOB);
+ jsc.picker.sldPtrOB.appendChild(jsc.picker.sldPtrMB);
+ jsc.picker.sldPtrMB.appendChild(jsc.picker.sldPtrIB);
+ jsc.picker.sldPtrIB.appendChild(jsc.picker.sldPtrS);
+ jsc.picker.box.appendChild(jsc.picker.sldB);
+ jsc.picker.box.appendChild(jsc.picker.sldM);
+
+ jsc.picker.btn.appendChild(jsc.picker.btnT);
+ jsc.picker.box.appendChild(jsc.picker.btn);
+
+ jsc.picker.boxB.appendChild(jsc.picker.box);
+ jsc.picker.wrap.appendChild(jsc.picker.boxS);
+ jsc.picker.wrap.appendChild(jsc.picker.boxB);
+ }
+
+ var p = jsc.picker;
+
+ var displaySlider = !!jsc.getSliderComponent(THIS);
+ var dims = jsc.getPickerDims(THIS);
+ var crossOuterSize = (2 * THIS.pointerBorderWidth + THIS.pointerThickness + 2 * THIS.crossSize);
+ var padToSliderPadding = jsc.getPadToSliderPadding(THIS);
+ var borderRadius = Math.min(
+ THIS.borderRadius,
+ Math.round(THIS.padding * Math.PI)); // px
+ var padCursor = 'crosshair';
+
+ // wrap
+ p.wrap.style.clear = 'both';
+ p.wrap.style.width = (dims[0] + 2 * THIS.borderWidth) + 'px';
+ p.wrap.style.height = (dims[1] + 2 * THIS.borderWidth) + 'px';
+ p.wrap.style.zIndex = THIS.zIndex;
+
+ // picker
+ p.box.style.width = dims[0] + 'px';
+ p.box.style.height = dims[1] + 'px';
+
+ p.boxS.style.position = 'absolute';
+ p.boxS.style.left = '0';
+ p.boxS.style.top = '0';
+ p.boxS.style.width = '100%';
+ p.boxS.style.height = '100%';
+ jsc.setBorderRadius(p.boxS, borderRadius + 'px');
+
+ // picker border
+ p.boxB.style.position = 'relative';
+ p.boxB.style.border = THIS.borderWidth + 'px solid';
+ p.boxB.style.borderColor = THIS.borderColor;
+ p.boxB.style.background = THIS.backgroundColor;
+ jsc.setBorderRadius(p.boxB, borderRadius + 'px');
+
+ // IE hack:
+ // If the element is transparent, IE will trigger the event on the elements under it,
+ // e.g. on Canvas or on elements with border
+ p.padM.style.background =
+ p.sldM.style.background =
+ '#FFF';
+ jsc.setStyle(p.padM, 'opacity', '0');
+ jsc.setStyle(p.sldM, 'opacity', '0');
+
+ // pad
+ p.pad.style.position = 'relative';
+ p.pad.style.width = THIS.width + 'px';
+ p.pad.style.height = THIS.height + 'px';
+
+ // pad palettes (HSV and HVS)
+ p.padPal.draw(THIS.width, THIS.height, jsc.getPadYComponent(THIS));
+
+ // pad border
+ p.padB.style.position = 'absolute';
+ p.padB.style.left = THIS.padding + 'px';
+ p.padB.style.top = THIS.padding + 'px';
+ p.padB.style.border = THIS.insetWidth + 'px solid';
+ p.padB.style.borderColor = THIS.insetColor;
+
+ // pad mouse area
+ p.padM._jscInstance = THIS;
+ p.padM._jscControlName = 'pad';
+ p.padM.style.position = 'absolute';
+ p.padM.style.left = '0';
+ p.padM.style.top = '0';
+ p.padM.style.width = (THIS.padding + 2 * THIS.insetWidth + THIS.width + padToSliderPadding / 2) + 'px';
+ p.padM.style.height = dims[1] + 'px';
+ p.padM.style.cursor = padCursor;
+
+ // pad cross
+ p.cross.style.position = 'absolute';
+ p.cross.style.left =
+ p.cross.style.top =
+ '0';
+ p.cross.style.width =
+ p.cross.style.height =
+ crossOuterSize + 'px';
+
+ // pad cross border Y and X
+ p.crossBY.style.position =
+ p.crossBX.style.position =
+ 'absolute';
+ p.crossBY.style.background =
+ p.crossBX.style.background =
+ THIS.pointerBorderColor;
+ p.crossBY.style.width =
+ p.crossBX.style.height =
+ (2 * THIS.pointerBorderWidth + THIS.pointerThickness) + 'px';
+ p.crossBY.style.height =
+ p.crossBX.style.width =
+ crossOuterSize + 'px';
+ p.crossBY.style.left =
+ p.crossBX.style.top =
+ (Math.floor(crossOuterSize / 2) - Math.floor(THIS.pointerThickness / 2) - THIS.pointerBorderWidth) + 'px';
+ p.crossBY.style.top =
+ p.crossBX.style.left =
+ '0';
+
+ // pad cross line Y and X
+ p.crossLY.style.position =
+ p.crossLX.style.position =
+ 'absolute';
+ p.crossLY.style.background =
+ p.crossLX.style.background =
+ THIS.pointerColor;
+ p.crossLY.style.height =
+ p.crossLX.style.width =
+ (crossOuterSize - 2 * THIS.pointerBorderWidth) + 'px';
+ p.crossLY.style.width =
+ p.crossLX.style.height =
+ THIS.pointerThickness + 'px';
+ p.crossLY.style.left =
+ p.crossLX.style.top =
+ (Math.floor(crossOuterSize / 2) - Math.floor(THIS.pointerThickness / 2)) + 'px';
+ p.crossLY.style.top =
+ p.crossLX.style.left =
+ THIS.pointerBorderWidth + 'px';
+
+ // slider
+ p.sld.style.overflow = 'hidden';
+ p.sld.style.width = THIS.sliderSize + 'px';
+ p.sld.style.height = THIS.height + 'px';
+
+ // slider gradient
+ p.sldGrad.draw(THIS.sliderSize, THIS.height, '#000', '#000');
+
+ // slider border
+ p.sldB.style.display = displaySlider ? 'block' : 'none';
+ p.sldB.style.position = 'absolute';
+ p.sldB.style.right = THIS.padding + 'px';
+ p.sldB.style.top = THIS.padding + 'px';
+ p.sldB.style.border = THIS.insetWidth + 'px solid';
+ p.sldB.style.borderColor = THIS.insetColor;
+
+ // slider mouse area
+ p.sldM._jscInstance = THIS;
+ p.sldM._jscControlName = 'sld';
+ p.sldM.style.display = displaySlider ? 'block' : 'none';
+ p.sldM.style.position = 'absolute';
+ p.sldM.style.right = '0';
+ p.sldM.style.top = '0';
+ p.sldM.style.width = (THIS.sliderSize + padToSliderPadding / 2 + THIS.padding + 2 * THIS.insetWidth) + 'px';
+ p.sldM.style.height = dims[1] + 'px';
+ p.sldM.style.cursor = 'default';
+
+ // slider pointer inner and outer border
+ p.sldPtrIB.style.border =
+ p.sldPtrOB.style.border =
+ THIS.pointerBorderWidth + 'px solid ' + THIS.pointerBorderColor;
+
+ // slider pointer outer border
+ p.sldPtrOB.style.position = 'absolute';
+ p.sldPtrOB.style.left = -(2 * THIS.pointerBorderWidth + THIS.pointerThickness) + 'px';
+ p.sldPtrOB.style.top = '0';
+
+ // slider pointer middle border
+ p.sldPtrMB.style.border = THIS.pointerThickness + 'px solid ' + THIS.pointerColor;
+
+ // slider pointer spacer
+ p.sldPtrS.style.width = THIS.sliderSize + 'px';
+ p.sldPtrS.style.height = sliderPtrSpace + 'px';
+
+ // the Close button
+ function setBtnBorder () {
+ var insetColors = THIS.insetColor.split(/\s+/);
+ var outsetColor = insetColors.length < 2 ? insetColors[0] : insetColors[1] + ' ' + insetColors[0] + ' ' + insetColors[0] + ' ' + insetColors[1];
+ p.btn.style.borderColor = outsetColor;
+ }
+ p.btn.style.display = THIS.closable ? 'block' : 'none';
+ p.btn.style.position = 'absolute';
+ p.btn.style.left = THIS.padding + 'px';
+ p.btn.style.bottom = THIS.padding + 'px';
+ p.btn.style.padding = '0 15px';
+ p.btn.style.height = THIS.buttonHeight + 'px';
+ p.btn.style.border = THIS.insetWidth + 'px solid';
+ setBtnBorder();
+ p.btn.style.color = THIS.buttonColor;
+ p.btn.style.font = '12px sans-serif';
+ p.btn.style.textAlign = 'center';
+ try {
+ p.btn.style.cursor = 'pointer';
+ } catch(eOldIE) {
+ p.btn.style.cursor = 'hand';
+ }
+ p.btn.onmousedown = function () {
+ THIS.hide();
+ };
+ p.btnT.style.lineHeight = THIS.buttonHeight + 'px';
+ p.btnT.innerHTML = '';
+ p.btnT.appendChild(document.createTextNode(THIS.closeText));
+
+ // place pointers
+ redrawPad();
+ redrawSld();
+
+ // If we are changing the owner without first closing the picker,
+ // make sure to first deal with the old owner
+ if (jsc.picker.owner && jsc.picker.owner !== THIS) {
+ jsc.unsetClass(jsc.picker.owner.targetElement, THIS.activeClass);
+ }
+
+ // Set the new picker owner
+ jsc.picker.owner = THIS;
+
+ // The redrawPosition() method needs picker.owner to be set, that's why we call it here,
+ // after setting the owner
+ if (jsc.isElementType(container, 'body')) {
+ jsc.redrawPosition();
+ } else {
+ jsc._drawPosition(THIS, 0, 0, 'relative', false);
+ }
+
+ if (p.wrap.parentNode != container) {
+ container.appendChild(p.wrap);
+ }
+
+ jsc.setClass(THIS.targetElement, THIS.activeClass);
+ }
+
+
+ function redrawPad () {
+ // redraw the pad pointer
+ switch (jsc.getPadYComponent(THIS)) {
+ case 's': var yComponent = 1; break;
+ case 'v': var yComponent = 2; break;
+ }
+ var x = Math.round((THIS.hsv[0] / 360) * (THIS.width - 1));
+ var y = Math.round((1 - THIS.hsv[yComponent] / 100) * (THIS.height - 1));
+ var crossOuterSize = (2 * THIS.pointerBorderWidth + THIS.pointerThickness + 2 * THIS.crossSize);
+ var ofs = -Math.floor(crossOuterSize / 2);
+ jsc.picker.cross.style.left = (x + ofs) + 'px';
+ jsc.picker.cross.style.top = (y + ofs) + 'px';
+
+ // redraw the slider
+ switch (jsc.getSliderComponent(THIS)) {
+ case 's':
+ var rgb1 = HSV_RGB(THIS.hsv[0], 100, THIS.hsv[2]);
+ var rgb2 = HSV_RGB(THIS.hsv[0], 0, THIS.hsv[2]);
+ var color1 = 'rgb(' +
+ Math.round(rgb1[0]) + ',' +
+ Math.round(rgb1[1]) + ',' +
+ Math.round(rgb1[2]) + ')';
+ var color2 = 'rgb(' +
+ Math.round(rgb2[0]) + ',' +
+ Math.round(rgb2[1]) + ',' +
+ Math.round(rgb2[2]) + ')';
+ jsc.picker.sldGrad.draw(THIS.sliderSize, THIS.height, color1, color2);
+ break;
+ case 'v':
+ var rgb = HSV_RGB(THIS.hsv[0], THIS.hsv[1], 100);
+ var color1 = 'rgb(' +
+ Math.round(rgb[0]) + ',' +
+ Math.round(rgb[1]) + ',' +
+ Math.round(rgb[2]) + ')';
+ var color2 = '#000';
+ jsc.picker.sldGrad.draw(THIS.sliderSize, THIS.height, color1, color2);
+ break;
+ }
+ }
+
+
+ function redrawSld () {
+ var sldComponent = jsc.getSliderComponent(THIS);
+ if (sldComponent) {
+ // redraw the slider pointer
+ switch (sldComponent) {
+ case 's': var yComponent = 1; break;
+ case 'v': var yComponent = 2; break;
+ }
+ var y = Math.round((1 - THIS.hsv[yComponent] / 100) * (THIS.height - 1));
+ jsc.picker.sldPtrOB.style.top = (y - (2 * THIS.pointerBorderWidth + THIS.pointerThickness) - Math.floor(sliderPtrSpace / 2)) + 'px';
+ }
+ }
+
+
+ function isPickerOwner () {
+ return jsc.picker && jsc.picker.owner === THIS;
+ }
+
+
+ function blurValue () {
+ THIS.importColor();
+ }
+
+
+ // Find the target element
+ if (typeof targetElement === 'string') {
+ var id = targetElement;
+ var elm = document.getElementById(id);
+ if (elm) {
+ this.targetElement = elm;
+ } else {
+ jsc.warn('Could not find target element with ID \'' + id + '\'');
+ }
+ } else if (targetElement) {
+ this.targetElement = targetElement;
+ } else {
+ jsc.warn('Invalid target element: \'' + targetElement + '\'');
+ }
+
+ if (this.targetElement._jscLinkedInstance) {
+ jsc.warn('Cannot link jscolor twice to the same element. Skipping.');
+ return;
+ }
+ this.targetElement._jscLinkedInstance = this;
+
+ // Find the value element
+ this.valueElement = jsc.fetchElement(this.valueElement);
+ // Find the style element
+ this.styleElement = jsc.fetchElement(this.styleElement);
+
+ var THIS = this;
+ var container =
+ this.container ?
+ jsc.fetchElement(this.container) :
+ document.getElementsByTagName('body')[0];
+ var sliderPtrSpace = 3; // px
+
+ // For BUTTON elements it's important to stop them from sending the form when clicked
+ // (e.g. in Safari)
+ if (jsc.isElementType(this.targetElement, 'button')) {
+ if (this.targetElement.onclick) {
+ var origCallback = this.targetElement.onclick;
+ this.targetElement.onclick = function (evt) {
+ origCallback.call(this, evt);
+ return false;
+ };
+ } else {
+ this.targetElement.onclick = function () { return false; };
+ }
+ }
+
+ /*
+ var elm = this.targetElement;
+ do {
+ // If the target element or one of its offsetParents has fixed position,
+ // then use fixed positioning instead
+ //
+ // Note: In Firefox, getComputedStyle returns null in a hidden iframe,
+ // that's why we need to check if the returned style object is non-empty
+ var currStyle = jsc.getStyle(elm);
+ if (currStyle && currStyle.position.toLowerCase() === 'fixed') {
+ this.fixed = true;
+ }
+
+ if (elm !== this.targetElement) {
+ // attach onParentScroll so that we can recompute the picker position
+ // when one of the offsetParents is scrolled
+ if (!elm._jscEventsAttached) {
+ jsc.attachEvent(elm, 'scroll', jsc.onParentScroll);
+ elm._jscEventsAttached = true;
+ }
+ }
+ } while ((elm = elm.offsetParent) && !jsc.isElementType(elm, 'body'));
+ */
+
+ // valueElement
+ if (this.valueElement) {
+ if (jsc.isElementType(this.valueElement, 'input')) {
+ var updateField = function () {
+ THIS.fromString(THIS.valueElement.value, jsc.leaveValue);
+ jsc.dispatchFineChange(THIS);
+ };
+ jsc.attachEvent(this.valueElement, 'keyup', updateField);
+ jsc.attachEvent(this.valueElement, 'input', updateField);
+ jsc.attachEvent(this.valueElement, 'blur', blurValue);
+ this.valueElement.setAttribute('autocomplete', 'off');
+ }
+ }
+
+ // styleElement
+ if (this.styleElement) {
+ this.styleElement._jscOrigStyle = {
+ backgroundImage : this.styleElement.style.backgroundImage,
+ backgroundColor : this.styleElement.style.backgroundColor,
+ color : this.styleElement.style.color
+ };
+ }
+
+ if (this.value) {
+ // Try to set the color from the .value option and if unsuccessful,
+ // export the current color
+ this.fromString(this.value) || this.exportColor();
+ } else {
+ this.importColor();
+ }
+ }
+
+};
+
+
+//================================
+// Public properties and methods
+//================================
+
+
+// By default, search for all elements with class="jscolor" and install a color picker on them.
+//
+// You can change what class name will be looked for by setting the property jscolor.lookupClass
+// anywhere in your HTML document. To completely disable the automatic lookup, set it to null.
+//
+jsc.jscolor.lookupClass = 'jscolor';
+
+
+jsc.jscolor.installByClassName = function (className) {
+ var inputElms = document.getElementsByTagName('input');
+ var buttonElms = document.getElementsByTagName('button');
+
+ jsc.tryInstallOnElements(inputElms, className);
+ jsc.tryInstallOnElements(buttonElms, className);
+};
+
+
+jsc.register();
+
+
+return jsc.jscolor;
+
+
+})(); }
diff --git a/src/scripts/log.js b/src/scripts/log.js
new file mode 100644
index 0000000..5641fa8
--- /dev/null
+++ b/src/scripts/log.js
@@ -0,0 +1,151 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ // We either came from /options.html or were opened as a new tab from popup.html (in that case, do nothing)
+ history.back();
+ }
+});
+
+// ----------------- Spinner -------------------------------
+const spinner = document.querySelector('.spinner');
+function hideSpinner() {
+
+ spinner.classList.remove('on');
+ setTimeout(() => { spinner.style.display = 'none'; }, 600);
+}
+
+function showSpinner() {
+
+ spinner.style.display = 'flex';
+ spinner.classList.add('on');
+}
+// ----------------- /spinner ------------------------------
+
+// ----- global
+let logger;
+const onOff = document.querySelector('#onOff');
+const logSize = document.querySelector('#logSize');
+
+chrome.runtime.getBackgroundPage(bg => {
+
+ logger = bg.getLog();
+ onOff.checked = logger.active;
+ logSize.value = logger.size;
+ renderMatchedLog(); // log content will be shown if there are any, regardless of onOff
+ renderUnmatchedLog(); // log content will be shown if there are any, regardless of onOff
+ hideSpinner();
+});
+
+onOff.addEventListener('change', (e) => {
+
+ logger.active = onOff.checked;
+ logger.updateStorage();
+});
+
+logSize.addEventListener('change', (e) => {
+
+ logSize.value = logSize.value*1 || logger.size; // defaults on bad number entry
+ if (logger.size !== logSize.value) { // update on change
+ logger.size = logSize.value;
+ logger.updateStorage();
+ }
+});
+
+document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));
+
+function process () {
+
+ switch (this.dataset.i18n) {
+
+ case 'back': location.href = '/options.html'; break;
+ case 'refresh':
+ renderMatchedLog();
+ renderUnmatchedLog();
+ break;
+ case 'clear':
+ logger.clear();
+ renderMatchedLog();
+ renderUnmatchedLog();
+ break;
+ }
+}
+
+function renderMatchedLog() {
+
+ // ----- templates & containers
+ const docfrag = document.createDocumentFragment();
+ const tr = document.querySelector('tr.matchedtemplate');
+ const tbody = tr.parentNode.nextElementSibling;
+ tbody.textContent = ''; // clearing the content
+
+ const forAll = chrome.i18n.getMessage('forAll');
+ const NA = chrome.i18n.getMessage('notApplicable');
+
+ logger.matchedList.forEach(item => {
+
+ const pattern = item.matchedPattern ?
+ (item.matchedPattern === 'all' ? forAll : item.matchedPattern) : 'No matches';
+
+ // Build a row for this log entry by cloning the tr containing 7 td
+ const row = tr.cloneNode(true);
+ row.className = item.matchedPattern ? 'success' : 'secondary'; // this will rest class .tamplate as well
+ const td = row.children;
+
+ const a = td[0].children[0];
+ a.href = item.url;
+ a.textContent = item.url;
+
+ td[1].textContent = item.title || NA;
+ td[2].style.backgroundColor = item.color || 'blue';
+ td[3].textContent = item.address || NA;
+ td[4].textContent = pattern;
+ td[5].textContent = item.whiteBlack || NA;
+ td[6].textContent = formatInt(item.timestamp);
+
+ docfrag.appendChild(row);
+ });
+
+ tbody.appendChild(docfrag);
+}
+
+function renderUnmatchedLog() {
+
+ // ----- templates & containers
+ const docfrag = document.createDocumentFragment();
+ const tr = document.querySelector('tr.unmatchedtemplate');
+ const tbody = tr.parentNode.nextElementSibling;
+ tbody.textContent = ''; // clearing the content
+
+ logger.unmatchedList.forEach(item => {
+ // Build a row for this log entry by cloning the tr containing 2 td
+ const row = tr.cloneNode(true);
+ const td = row.children;
+ const a = td[0].children[0];
+
+ a.href = item.url;
+ a.textContent = item.url;
+ td[1].textContent = formatInt(item.timestamp);
+
+ docfrag.appendChild(row);
+ });
+
+ tbody.appendChild(docfrag);
+}
+
+function formatInt(d) {
+ // International format based on user locale
+ // you can delete the other function if you like this
+ // you can adjust the content via the object properties
+ return new Intl.DateTimeFormat(navigator.language,
+ {weekday: 'short', year: 'numeric', month: 'short', day: 'numeric',
+ hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false}).format(new Date(d));
+}
diff --git a/src/scripts/matcher.js b/src/scripts/matcher.js
new file mode 100644
index 0000000..19cd49d
--- /dev/null
+++ b/src/scripts/matcher.js
@@ -0,0 +1,116 @@
+'use strict';
+
+const schemeSet = {
+ all : 1,
+ http: 2,
+ https: 4
+};
+// Shortcuts so we dont perform i18n lookups for every non-match
+const FOR_ALL = {originalPattern: chrome.i18n.getMessage('forAll')}
+const NOMATCH_TEXT = chrome.i18n.getMessage('noMatch');
+const NONE_TEXT = chrome.i18n.getMessage('none');
+const NOMATCH_COLOR = '#D3D3D3';
+const WHITE = chrome.i18n.getMessage('white');
+const BLACK = chrome.i18n.getMessage('black');
+
+function findProxyMatch(url, activeSettings) {
+ // note: we've already thrown out inactive settings and inactive patterns in background.js.
+ // we're not iterating over them
+
+ if (activeSettings.mode === 'patterns') {
+ // Unfortunately, since Firefox 57 and some releases afterwards, we were unable
+ // to get anything of the URL except scheme, port, and host (because of Fx's PAC
+ // implementation). Now we have access to rest of URL, like pre-57, but users
+ // have written their patterns not anticipating that. Need to do more research
+ // before using other parts of URL. For now, we ignore the other parts.
+ const parsedUrl = new URL(url);
+ const scheme = parsedUrl.protocol.substring(0, parsedUrl.protocol.length-1); // strip the colon
+ const hostPort = parsedUrl.host; // This includes port if one is specified
+
+ for (const proxy of activeSettings.proxySettings) {
+
+ // Check black patterns first
+ const blackMatch = proxy.blackPatterns.find(item =>
+ (item.protocols === schemeSet.all || item.protocols === schemeSet[scheme]) &&
+ item.pattern.test(hostPort));
+
+ if (blackMatch) {
+ sendToMatchedLog(url, proxy, Utils.getProxyTitle(proxy), blackMatch, BLACK);
+ continue; // if blacklist matched, continue to the next proxy
+ }
+
+ const whiteMatch = proxy.whitePatterns.find(item =>
+ (item.protocols === schemeSet.all || item.protocols === schemeSet[scheme]) &&
+ item.pattern.test(hostPort));
+
+ if (whiteMatch) {
+ // found a whitelist match, end here
+ const title = Utils.getProxyTitle(proxy);
+ Utils.updateIcon('images/icon.svg', proxy.color, title, false, title, false);
+ sendToMatchedLog(url, proxy, title, whiteMatch, WHITE);
+ return prepareSetting(proxy);
+ }
+ }
+ // no white matches in any settings
+ sendToUnmatchedLog(url);
+ Utils.updateIcon('images/gray.svg', null, NOMATCH_TEXT, false, NOMATCH_TEXT, false);
+ return {type: 'direct'};
+ }
+ else if (activeSettings.mode === 'disabled') {
+ // Generally we won't get to this block because our proxy handler is turned off in this mode.
+ // We will get here at startup and also if there is a race condition between removing our listener
+ // (when switching to disabled mode) and handaling requests.
+ return {type: 'direct'};
+ }
+ else {
+ // Fixed mode -- use 1 proxy for all URLs
+ const p = activeSettings.proxySettings[0];
+ const title = Utils.getProxyTitle(p);
+ Utils.updateIcon('images/icon.svg', p.color, title, false, title, false);
+ sendToMatchedLog(url, p, title, FOR_ALL);
+ return prepareSetting(p);
+ }
+}
+
+const typeSet = {
+ 1: 'http', // PROXY_TYPE_HTTP
+ 2: 'https', // PROXY_TYPE_HTTPS
+ 3: 'socks', // PROXY_TYPE_SOCKS5
+ 4: 'socks4', // PROXY_TYPE_SOCKS4
+ 5: 'direct' // PROXY_TYPE_NONE
+};
+
+function prepareSetting(proxy) {
+ const ret = {
+ type: typeSet[proxy.type] || typeSet[5], // If 'direct', all other properties of this object are ignored.
+ host: proxy.address,
+ port: proxy.port
+ };
+ proxy.username && (ret.username = proxy.username);
+ proxy.password && (ret.password = proxy.password);
+ proxy.proxyDNS && (ret.proxyDNS = proxy.proxyDNS); // Only useful for SOCKS
+ //if ((proxy.type === PROXY_TYPE_HTTP || proxy.type === PROXY_TYPE_HTTPS) && proxy.username && proxy.password) {
+ // Using wireshark, I do not see this header being sent, contrary to
+ // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy/ProxyInfo
+ //ret.proxyAuthorizationHeader = 'Basic ' + btoa(proxy.username + ":" + proxy.password);
+ //}
+ return ret;
+}
+
+function sendToMatchedLog(url, proxy, title, matchedPattern, whiteBlack) {
+ // log only the data that is needed for display
+ logger && logger.active && logger.addMatched({
+ url,
+ title,
+ color: proxy.color,
+ address: proxy.address,
+ // Log should display whatever user typed, not our processed version of the pattern
+ matchedPattern: matchedPattern.originalPattern,
+ whiteBlack,
+ timestamp: Date.now()
+ });
+}
+
+function sendToUnmatchedLog(url) {
+ logger && logger.active && logger.addUnmatched({url, timestamp: Date.now()});
+}
diff --git a/src/scripts/options.js b/src/scripts/options.js
new file mode 100644
index 0000000..14d7eab
--- /dev/null
+++ b/src/scripts/options.js
@@ -0,0 +1,380 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+// ----- global
+const accounts = document.querySelector('#accounts');
+const mode = document.querySelector('#mode');
+const syncOnOff = document.querySelector('#syncOnOff');
+const popup = document.querySelector('.popup');
+const popupMain = popup.children[0];
+
+let storageArea, minIndex = Number.MAX_SAFE_INTEGER;
+
+// ----------------- User Preference -----------------------
+chrome.storage.local.get(null, result => {
+ // if sync is NOT set or it is false, use this result
+ syncOnOff.checked = result.sync;
+ localStorage.setItem('sync', syncOnOff.checked);
+ storageArea = result.sync ? chrome.storage.sync : chrome.storage.local;
+ result.sync ? chrome.storage.sync.get(null, processOptions) : processOptions(result);
+});
+// ----------------- /User Preference ----------------------
+
+// ----------------- Spinner -------------------------------
+const spinner = document.querySelector('.spinner');
+function hideSpinner() {
+
+ spinner.classList.remove('on');
+ setTimeout(() => { spinner.style.display = 'none'; }, 600);
+}
+
+function showSpinner() {
+
+ spinner.style.display = 'flex';
+ spinner.classList.add('on');
+}
+// ----------------- /spinner ------------------------------
+
+
+// ----- add Listeners for menu
+document.querySelectorAll('nav a').forEach(item => item.addEventListener('click', process));
+function process() {
+
+ switch (this.dataset.i18n) {
+
+ case 'add':
+ localStorage.removeItem('id'); // clear localStorage; this indicates an add not an edit
+ localStorage.setItem('nextIndex', minIndex); // index to use for this proxy so that it's added to the beginning
+ location.href = '/proxy.html';
+ break;
+ case 'export': Utils.exportFile(); break;
+ case 'import': location.href = '/import.html'; break;
+ case 'importProxyList': location.href = '/import-proxy-list.html'; break;
+ case 'log': location.href = '/log.html'; break;
+ case 'about': location.href = '/about.html'; break;
+
+ case 'deleteAll':
+ if (confirm(chrome.i18n.getMessage('confirmDelete'))) {
+ showSpinner();
+ chrome.storage.local.clear(() => chrome.storage.sync.clear(() => {
+ hideSpinner();
+ Utils.notify(chrome.i18n.getMessage('deleteAllmessage'));
+ location.href = '/options.html';
+ }));
+ }
+ break;
+
+ case 'deleteBrowserData':
+ const h4 = document.createElement('h4');
+ const p = document.createElement('p');
+ popupMain.children[0].textContent = chrome.i18n.getMessage('deleteBrowserData');
+ let h = h4.cloneNode();
+ h.textContent = chrome.i18n.getMessage('deleteNot');
+ let p1 = p.cloneNode();
+ p1.textContent = chrome.i18n.getMessage('deleteBrowserDataNotDescription');
+ popupMain.children[1].appendChild(h);
+ popupMain.children[1].appendChild(p1);
+
+ h = h4.cloneNode();
+ h.textContent = chrome.i18n.getMessage('delete');
+ p1 = p.cloneNode();
+ p1.textContent = chrome.i18n.getMessage('deleteBrowserDataDescription');
+ popupMain.children[1].appendChild(h);
+ popupMain.children[1].appendChild(p1);
+
+ popupMain.children[2].children[0].addEventListener('click', closePopup);
+ popupMain.children[2].children[1].addEventListener('click', () => // Not cancelled
+ chrome.browsingData.remove({}, {
+ //appcache: true,
+ cache: true,
+ cookies: true,
+ downloads: false,
+ //fileSystems: true,
+ formData: false,
+ history: false,
+ indexedDB: true,
+ localStorage: true,
+ pluginData: true,
+ //passwords: true,
+ //webSQL: true,
+ //serverBoundCertificates: true,
+ serviceWorkers: true
+ }, () => {
+ Utils.notify(chrome.i18n.getMessage('done'));
+ closePopup();
+ }
+ ));
+ showPopup();
+ break;
+ }
+}
+
+// ----- add Listeners for initial elements
+mode.addEventListener('change', selectMode);
+function selectMode() {
+
+ // set color
+ mode.style.color = mode.children[mode.selectedIndex].style.color;
+
+ console.log(mode, "selectMode");
+ // we already know the state of sync | this is set when manually changing the select
+ // it is undefined when mode is switched from toolbar popup or on startup
+ this && storageArea.set({mode: mode.value});
+
+ // --- change the state of success/secondary
+ // change all success -> secondary
+ document.querySelectorAll('.success').forEach(item => item.classList.replace('success', 'secondary'));
+
+ switch (mode.value) {
+
+ case 'patterns':
+ document.querySelectorAll('input[name="onOff"]:checked').forEach(item => {
+ const node = item.parentNode.parentNode;
+ node.classList.replace('secondary', 'success'); // FF49, Ch 61
+ });
+ break;
+
+ case 'disabled': // do nothing
+ break;
+
+ default:
+ const node = document.getElementById(mode.value);
+ node && node.classList.replace('secondary', 'success');
+ }
+}
+
+syncOnOff.addEventListener('change', () => {
+ const useSync = syncOnOff.checked;
+ // sync value always CHECKED locally
+ // data is merged, replacing exisitng and adding new ones
+ localStorage.setItem('sync', syncOnOff.checked);
+ storageArea = syncOnOff.checked ? chrome.storage.sync : chrome.storage.local;
+ if (useSync && confirm(chrome.i18n.getMessage('confirmTransferToSync'))) {
+ showSpinner();
+ chrome.storage.local.set({sync: true}); // save sync state
+ chrome.storage.local.get(null, result => { // get source
+ delete result.sync;
+ chrome.storage.sync.set(result, hideSpinner); // save to target
+ }); // get source & save to target
+ }
+ else if (!useSync && confirm(chrome.i18n.getMessage('confirmTransferToLocal'))) {
+ showSpinner();
+ chrome.storage.sync.get(null, result => { // get source
+ result.sync = false; // set sync = false
+ chrome.storage.local.set(result, hideSpinner); // save to target
+ });
+ }
+});
+
+
+chrome.runtime.onMessage.addListener((message, sender) => { // from popup or bg
+// console.log(message);
+ if(!message.mode || message.mode === mode.value) { return; } // change if it is different
+ mode.value = message.mode;
+ selectMode();
+});
+
+function processOptions(pref) {
+ // --- reset
+ accounts.textContent = '';
+
+ // remove all <option> elements except patterns and disabled
+ [...mode.children].forEach(item => !['patterns', 'disabled'].includes(item.value) && item.remove());
+
+ // ----- templates & containers
+ const docfrag = document.createDocumentFragment();
+ const docfrag2 = document.createDocumentFragment();
+ const temp = document.querySelector('.template');
+
+ // --- working directly with DB format
+
+ // add default lastresort if not there
+ //pref[LASTRESORT] || (pref[LASTRESORT] = DEFAULT_PROXY_SETTING);
+
+ const prefKeys = Object.keys(pref).filter(item => !NON_PROXY_KEYS.includes(item)); // not for these
+
+ prefKeys.sort((a, b) => pref[a].index - pref[b].index); // sort by index
+ if (prefKeys[0]) {
+ minIndex = pref[prefKeys[0]].index; // the first index after sort (if any)
+ }
+
+ pref.mode = pref.mode || 'disabled'; // defaults to disabled
+ prefKeys.forEach(id => {
+ const item = pref[id];
+
+ const div = temp.cloneNode(true);
+ const node = [...div.children[0].children, ...div.children[1].children];
+ div.classList.remove('template');
+ //id === LASTRESORT && div.children[1].classList.add('default');
+
+ div.id = id;
+ node[0].style.backgroundColor = item.color;
+ node[1].textContent = Utils.getProxyTitle(item);
+ node[2].textContent = item.address; // ellipsis is handled by CSS
+ if (item.cc) {
+ node[3].classList.remove('hide');
+ node[3].textContent = getFlag(item.cc);
+ node[3].title = item.country;
+ }
+ item.username && item.password && node[4].classList.add('on');
+ node[5].id = id + '-onoff';
+ node[5].checked = item.active;
+ node[6].setAttribute('for', node[5].id);
+
+ FOXYPROXY_BASIC && (node[8].style.display = 'none');
+
+ // setting div colors
+ switch (true) {
+
+ case Utils.isUnsupportedType(item.type):
+ div.classList.add('unsupported');
+ break;
+
+ case pref.mode === 'patterns':
+ case pref.mode === 'random':
+ case pref.mode === 'roundrobin':
+ div.classList.add(item.active ? 'success' : 'secondary');
+ break;
+
+ case pref.mode === 'disabled':
+ div.classList.add('secondary');
+ break;
+
+ default:
+ div.classList.add(pref.mode == id ? 'success' : 'secondary');
+ }
+
+ docfrag.appendChild(div);
+
+ // add to select
+ const opt = new Option(node[1].textContent, id);
+ opt.style.color = item.color;
+ docfrag2.appendChild(opt);
+ });
+
+ docfrag.hasChildNodes() && accounts.appendChild(docfrag);
+ docfrag2.hasChildNodes() && mode.appendChild(docfrag2, mode.lastElementChild);
+
+ if (FOXYPROXY_BASIC) {
+ mode.children[0].classList.add('hide'); // hide by pattern option
+ pref.mode === 'patterns' && (pref.mode = 'disabled');
+ }
+
+ const opt = mode.querySelector(`option[value="${pref.mode}"]`);
+ if (opt) {
+ opt.selected = true;
+ mode.style.color = opt.style.color;
+ }
+
+ // add Listeners
+ document.querySelectorAll('button').forEach(item => item.addEventListener('click', processButton));
+
+ document.querySelectorAll('input[name="onOff"]').forEach(item => item.addEventListener('change', function() {
+ const id = this.parentNode.parentNode.id;
+ storageArea.get(id, result => {
+ result[id].active = this.checked;
+ storageArea.set(result);
+ });
+ }));
+
+ doWeHaveProxiesDefined();
+ hideSpinner();
+}
+
+function doWeHaveProxiesDefined() {
+ if (!accounts.hasChildNodes()) {
+ document.querySelector('#help').style.display = 'block';
+ document.querySelector('#rightColumn').classList.add('secondary');
+ document.querySelector('#mode').style.display = 'none';
+ }
+ else {
+ document.querySelector('#help').style.display = 'none';
+ document.querySelector('#rightColumn').classList.remove('warning');
+ document.querySelector('#mode').style.display = 'flex';
+ }
+}
+
+function getFlag(cc) {
+
+ cc = /^[A-Z]{2}$/i.test(cc) && cc.toUpperCase();
+ return cc && String.fromCodePoint(...[...cc].map(c => c.charCodeAt() + 127397));
+}
+
+function processButton() {
+
+ const parent = this.parentNode.parentNode;
+ const id = parent.id;
+
+ switch (this.dataset.i18n) {
+
+ case 'help|title':
+ popupMain.children[0].textContent = chrome.i18n.getMessage('syncSettings');
+ popupMain.children[1].textContent = chrome.i18n.getMessage('syncSettingsHelp');
+ popupMain.children[2].children[0].style.visibility = 'hidden';
+ popupMain.children[2].children[1].addEventListener('click', closePopup);
+ showPopup();
+ break;
+
+ case 'edit':
+ localStorage.setItem('id', id);
+ location.href = '/proxy.html';
+ break;
+
+ case 'patterns':
+ localStorage.setItem('id', id);
+ location.href = '/patterns.html';
+ break;
+
+ case 'delete|title':
+ if (confirm(chrome.i18n.getMessage('confirmDelete'))) {
+ parent.style.opacity = 0;
+ setTimeout(() => { parent.remove(); doWeHaveProxiesDefined();}, 600); // remove row
+ storageArea.remove(id);
+ }
+ break;
+
+ case 'up|title':
+ case 'down|title':
+ const target = this.dataset.i18n === 'up|title' ? parent.previousElementSibling : parent.nextElementSibling;
+ const insert = this.dataset.i18n === 'up|title' ? target : target.nextElementSibling;
+ parent.parentNode.insertBefore(parent, insert);
+ parent.classList.add('on');
+ setTimeout(() => { parent.classList.remove('on'); }, 600);
+ storageArea.get(null, result => {
+ // re-index
+ //[...accounts.children].forEach((item, index) => item.id !== LASTRESORT && (result[item.id].index = index));
+ [...accounts.children].forEach((item, index) => result[item.id].index = index);
+ minIndex = 0; // minimum index is always 0 now
+ storageArea.set(result);
+ });
+ break;
+ }
+}
+
+function showPopup() {
+
+ popup.style.display = 'flex';
+ window.getComputedStyle(popup).opacity;
+ window.getComputedStyle(popup.children[0]).transform;
+ popup.classList.add('on');
+}
+
+function closePopup() {
+
+ popup.classList.remove('on');
+ setTimeout(() => {
+ popup.style.display = 'none';
+ // reset
+ popupMain.children[0].textContent = '';
+ popupMain.children[1].textContent = '';
+ popupMain.children[2].children[0].style.visibility = 'visible';
+ popupMain.replaceChild(popupMain.children[2].cloneNode(true), popupMain.children[2]); // cloning to remove listeners
+ }, 600);
+}
diff --git a/src/scripts/pattern-help.js b/src/scripts/pattern-help.js
new file mode 100644
index 0000000..c5d7be1
--- /dev/null
+++ b/src/scripts/pattern-help.js
@@ -0,0 +1,9 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
diff --git a/src/scripts/pattern-tester.js b/src/scripts/pattern-tester.js
new file mode 100644
index 0000000..83e0a26
--- /dev/null
+++ b/src/scripts/pattern-tester.js
@@ -0,0 +1,77 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+// --- global
+const url = document.querySelector('#url');
+const pattern = document.querySelector('#pattern');
+const type = document.querySelector('#type');
+const protocols = document.querySelector('#protocols');
+const result = document.querySelector('#result');
+
+
+document.querySelector('button[data-i18n="test"]').addEventListener('click', testPattern);
+
+
+// ----- check for Edit
+const pat = localStorage.getItem('pattern');
+if (pat) {
+
+ pattern.value = pat;
+ type.value = localStorage.getItem('type');
+ protocols.value = localStorage.getItem('protocols');
+
+ localStorage.removeItem('pattern');
+ localStorage.removeItem('type');
+ localStorage.removeItem('protocols');
+}
+
+
+function testPattern() {
+
+ // --- reset
+ url.classList.remove('invalid');
+ pattern.classList.remove('invalid');
+ result.classList.add('hide');
+ result.classList.remove('alert');
+
+ // --- trim text values
+ [url, pattern].forEach(item => item.value = item.value.trim());
+
+ // --- URL check
+ let parsedURL;
+ try { parsedURL = new URL(url.value); }
+ catch (e) {
+ url.classList.add('invalid');
+ showResult(e.message, true);
+ return;
+ }
+
+ // --- protocol check
+ const protocolSet = { // converting to meaningful terms
+ '1': ['http:', 'https:'],
+ '2': ['http:'],
+ '4': ['https:']
+ };
+
+ if (!protocolSet[protocols.value].includes(parsedURL.protocol)) {
+ showResult(chrome.i18n.getMessage('errorProtocol'), true);
+ return;
+ }
+
+
+ // --- pattern check
+ const regex = checkPattern(pattern, type);
+ if (!regex) { return; }
+
+ // --- pattern on URL check (pattern is valid)
+ regex.test(parsedURL.host) ? showResult(chrome.i18n.getMessage('patternMatch')) :
+ showResult(chrome.i18n.getMessage('patternNotMatch'), true);
+
+}
diff --git a/src/scripts/patterns.js b/src/scripts/patterns.js
new file mode 100644
index 0000000..a276a20
--- /dev/null
+++ b/src/scripts/patterns.js
@@ -0,0 +1,283 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ history.back(); // We either came from /proxy.html or /options.html
+ }
+});
+
+// ----------------- Spinner -------------------------------
+const spinner = document.querySelector('.spinner');
+function hideSpinner() {
+
+ spinner.classList.remove('on');
+ setTimeout(() => { spinner.style.display = 'none'; }, 600);
+}
+
+function showSpinner() {
+
+ spinner.style.display = 'flex';
+ spinner.classList.add('on');
+}
+// ----------------- /spinner ------------------------------
+
+// ----- global
+let proxy = {};
+const header = document.querySelector('.header');
+const tbody = document.querySelectorAll('tbody'); // there are 2
+const template = document.querySelector('tr.template');
+const docfrag = document.createDocumentFragment();
+
+const defaultPattern = {
+ title: '',
+ active: true,
+ pattern: '',
+ type: 1, // PATTERN_TYPE_WILDCARD,
+ protocols: 1 // PROTOCOL_ALL
+};
+
+
+const protocolSet = { // converting to meaningful terms
+ 1: 'All',
+ 2: 'HTTP',
+ 4: 'HTTPS'
+};
+
+const patternTypeSet = {
+ 1: 'wildcard',
+ 2: 'Reg Exp'
+}
+
+// ----- check for Edit
+const id = localStorage.getItem('id');
+const sync = localStorage.getItem('sync') === 'true';
+const storageArea = !sync ? chrome.storage.local : chrome.storage.sync;
+if (id) { // This is an edit operation
+
+ storageArea.get(id, result => {
+
+ if (!Object.keys(result).length) {
+/*
+ if (id === LASTRESORT) { // error prevention
+ proxy = DEFAULT_PROXY_SETTING;
+ processOptions();
+ return;
+ }*/
+ console.error('Unable to edit saved proxy (could not get existing settings)')
+ return;
+ }
+
+ proxy = result[id];
+ if (proxy.title) { header.textContent = chrome.i18n.getMessage('editPatternsFor', proxy.title); }
+ processOptions();
+ hideSpinner();
+ })
+}
+/*
+else {
+ // Error, shouldn't ever get here
+ hideSpinner();
+ document.querySelector('#error').classList.remove('hide');
+ document.querySelector('.main').classList.add('hide');
+ console.error("2: Unable to read saved proxy proxy (could not get existing settings)");
+}*/
+
+// --- processing all buttons
+document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));
+
+function process() {
+
+ switch (this.dataset.i18n) {
+
+ case 'back': // error
+ case 'cancel':
+ location.href = '/options.html';
+ break;
+
+ case 'exportPatterns': exportPatterns(); break;
+
+ case 'newWhite':
+ addNew(tbody[0], 'whitePatterns');
+ break;
+
+ case 'newBlack':
+ addNew(tbody[1], 'blackPatterns');
+ break;
+
+ case 'save':
+ checkOptions();
+ break;
+
+ case 'add':
+ if (typeof(this.dataset.black) !== 'undefined') {
+ proxy.blackPatterns.push(...blacklistSet);
+ processOptions();
+ }
+ else {
+ proxy.whitePatterns.push(PATTERN_ALL_WHITE);
+ processOptions();
+ }
+ break;
+ }
+}
+
+function processOptions() {
+
+ // clearing the content
+ tbody[0].textContent = '';
+ tbody[1].textContent = '';
+
+ proxy.whitePatterns.forEach((item, index) => docfrag.appendChild(makeRow(item, index, 'whitePatterns')));
+ docfrag.hasChildNodes() && tbody[0].appendChild(docfrag);
+
+ proxy.blackPatterns.forEach((item, index) => docfrag.appendChild(makeRow(item, index, 'blackPatterns')));
+ docfrag.hasChildNodes() && tbody[1].appendChild(docfrag);
+
+}
+
+function makeRow(pat, index, bw) {
+
+ const tr = template.cloneNode(true);
+ tr.classList.remove('template');
+ tr.classList.add(pat.active ? 'success' : 'secondary');
+ tr.dataset.idx = index;
+ tr.dataset.bw = bw; // black/white
+ const td = tr.children;
+
+
+ td[0].children[0].value = pat.title;
+ td[1].children[0].value = pat.pattern;
+ td[2].children[0].value = pat.type;
+ td[3].children[0].value = pat.protocols;
+ td[4].children[0].checked = pat.active;
+ td[4].children[0].id = bw + index;
+ td[4].children[1].setAttribute('for', td[4].children[0].id);
+
+ pat.importedPattern && td[5].children[0].classList.remove('hide');
+
+ // add Listeners();
+ [...td[5].children].forEach(item => item.addEventListener('click', processEdit));
+
+ return tr;
+}
+
+function addNew(parent, bw) {
+
+ const tr = makeRow(defaultPattern, parent.children.length, bw);
+ parent.appendChild(tr);
+ tr.children[1].children[0].focus();
+}
+
+function processEdit() {
+
+ const parent = this.parentNode.parentNode;
+ const idx = parent.dataset.idx *1;
+ const patternsArray = proxy[parent.dataset.bw]; // whitePatterns | blackPatterns
+
+ switch (this.dataset.i18n) {
+
+ case 'imported|title':
+ alert(chrome.i18n.getMessage('importedPattern') + ' \n\n' + patternsArray[idx].importedPattern);
+ break;
+
+ case 'patternTester|title':
+ const pat = patternsArray[idx];
+ if (pat) {
+ localStorage.setItem('pattern', pat.pattern);
+ localStorage.setItem('type', pat.type);
+ localStorage.setItem('protocols', pat.protocols);
+ }
+ chrome.tabs.create({url: '/pattern-tester.html'});
+ break;
+
+ case 'delete|title':
+ parent.style.opacity = 0;
+ setTimeout(() => { parent.remove(); }, 300); // remove row
+ break;
+ }
+}
+
+
+function checkOptions() {
+
+ const pxy = {
+ whitePatterns: [],
+ blackPatterns: []
+ };
+
+ // use for loop to be able to return early on error
+ for (const item of document.querySelectorAll('tr[data-idx]')) {
+
+ const td = item.children;
+
+ // --- trim text values
+ [td[0].children[0], td[1].children[0]].forEach(item => item.value = item.value.trim());
+
+ // test pattern
+ const regex = testPattern(td[1].children[0], td[2].children[0]);
+ if (!regex) { return; }
+
+ const bw = item.dataset.bw;
+ pxy[bw].push({
+ title: td[0].children[0].value,
+ pattern: td[1].children[0].value,
+ type: td[2].children[0].value *1,
+ protocols: td[3].children[0].value *1,
+ active: td[4].children[0].checked
+ });
+ }
+
+ // all patterns passed
+ proxy.whitePatterns = pxy.whitePatterns;
+ proxy.blackPatterns = pxy.blackPatterns;
+ storageArea.set({[id]: proxy}, () => location.href = '/options.html');
+}
+
+
+function testPattern(pattern, type) {
+
+ // --- reset
+ pattern.classList.remove('invalid');
+ result.classList.add('hide');
+ result.classList.remove('alert');
+
+ // --- pattern check
+ return checkPattern(pattern, type);
+}
+
+
+
+function exportPatterns() {
+
+ const tmpObject = {whitePatterns: proxy.whitePatterns, blackPatterns: proxy.blackPatterns};
+ const blob = new Blob([JSON.stringify(tmpObject, null, 2)], {type : 'text/plain'});
+ const filename = 'foxyproxy' + (proxy.title ? '-' + proxy.title : '') + '-patterns' + '_' + new Date().toISOString().substring(0, 10) + '.json';
+ chrome.downloads.download({
+ url: URL.createObjectURL(blob),
+ filename,
+ saveAs: true,
+ conflictAction: 'uniquify'
+ }, () => console.log('Export/download finished')); // wait for it to complete before returning
+}
+
+
+document.getElementById('file').addEventListener('change', processFileSelect);
+function processFileSelect(e) {
+
+ const file = e.target.files[0];
+
+ Utils.importFile(file, ['application/json'], 1024*1024*5, 'json', imported => {
+ proxy.whitePatterns = imported.whitePatterns;
+ proxy.blackPatterns = imported.blackPatterns;
+ processOptions();
+ Utils.notify(chrome.i18n.getMessage('importBW', [proxy.whitePatterns.length, proxy.blackPatterns.length]));
+ });
+}
diff --git a/src/scripts/popup.js b/src/scripts/popup.js
new file mode 100644
index 0000000..120e5ed
--- /dev/null
+++ b/src/scripts/popup.js
@@ -0,0 +1,112 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+// ----------------- User Preference -----------------------
+let storageArea;
+chrome.storage.local.get(null, result => {
+ storageArea = result.sync ? chrome.storage.sync : chrome.storage.local;
+ result.sync ? chrome.storage.sync.get(null, processOptions) : processOptions(result);
+});
+// ----------------- /User Preference ----------------------
+
+function processOptions(pref) {
+
+ // ----- templates & containers
+ const docfrag = document.createDocumentFragment();
+ const temp = document.querySelector('li.template');
+
+ // add default lastresort if not there
+ //pref[LASTRESORT] || (pref[LASTRESORT] = DEFAULT_PROXY_SETTING);
+
+ const prefKeys = Object.keys(pref).filter(item => !NON_PROXY_KEYS.includes(item)); // not for these
+
+ prefKeys.sort((a, b) => pref[a].index - pref[b].index); // sort by index
+
+ pref.mode = pref.mode || 'disabled'; // defaults to disabled
+ let hasProxySettings = false;
+ prefKeys.forEach(id => {
+
+ const item = pref[id];
+
+ if (!Utils.isUnsupportedType(item.type)) { // if supported
+
+ const li = temp.cloneNode(true);
+ li.classList.remove('template');
+ li.id = id;
+ li.style.color = item.color;
+ li.children[0].textContent = Utils.getProxyTitle(item);
+ li.children[1].textContent = '(' + chrome.i18n.getMessage('forAll') + ')';
+
+ docfrag.appendChild(li);
+ hasProxySettings = true;
+ }
+ });
+
+ docfrag.hasChildNodes() && temp.parentNode.appendChild(docfrag, temp.nextElementSibling);
+
+ if (FOXYPROXY_BASIC) {
+ temp.parentNode.children[0].classList.add('hide'); // hide by pattern option
+ pref.mode === 'patterns' && (pref.mode = 'disabled');
+ }
+
+ // hide the selections if there are no proxy settings defined
+ document.getElementById('scroll').style.display = hasProxySettings ? 'block' : 'none';
+
+ const node = document.getElementById(pref.mode); // querySelector error with selectors starting with number
+ node.classList.add('on');
+
+ // add Listeners
+ document.querySelectorAll('li, button').forEach(item => item.addEventListener('click', process));
+}
+
+function process() {
+
+ let tabs;
+ switch (this.dataset.i18n) {
+
+ case 'myIP':
+ chrome.tabs.create({url: 'https://getfoxyproxy.org/geoip/'}); // no need to wait for it
+ window.close();
+ break;
+
+ case 'log':
+ const url = chrome.runtime.getURL('log.html');
+ chrome.tabs.query({url}, tabs => { // find a log tab
+ tabs[0] ? chrome.tabs.update(tabs[0].id, {active: true}) : chrome.tabs.create({url}); // active existing tab OR open new tab
+ window.close();
+ });
+ break;
+
+ case 'options':
+ chrome.tabs.query({url: chrome.runtime.getURL('') + '*'}, tabs => {
+ if (!tabs[0]) {
+ chrome.runtime.openOptionsPage();
+ window.close();
+ return;
+ }
+ const tab = tabs.find(item => /(proxy|options|patterns)\.html/.test(item.url)); // find a option tab
+ tab ? chrome.tabs.update(tab.id, {active: true}) : chrome.tabs.update(tabs[0].id, {active: true, url: '/options.html'});
+ window.close();
+ });
+ break;
+
+ default:
+ // reset the old one
+ const old = document.querySelector('.on');
+ old && old.classList.remove('on');
+ this.classList.add('on');
+
+
+ storageArea.set({mode: this.id}); // keep it open for more action
+ // popup & options are the only place that can set mode
+ // sending message to option && bg, if it is open
+ chrome.runtime.sendMessage({mode: this.id});
+ }
+}
diff --git a/src/scripts/proxy.js b/src/scripts/proxy.js
new file mode 100644
index 0000000..16394b7
--- /dev/null
+++ b/src/scripts/proxy.js
@@ -0,0 +1,228 @@
+'use strict';
+
+// ----------------- Internationalization ------------------
+document.querySelectorAll('[data-i18n]').forEach(node => {
+ let [text, attr] = node.dataset.i18n.split('|');
+ text = chrome.i18n.getMessage(text);
+ attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
+});
+// ----------------- /Internationalization -----------------
+
+document.addEventListener('keyup', evt => {
+ if (evt.keyCode === 27) {
+ close();
+ }
+});
+
+// ----- global
+let proxy = {}, proxiesAdded = 0;
+const color = new jscolor('colorChooser', {uppercase: false, hash: true});
+color.fromString(DEFAULT_COLOR); // starting from default color
+
+const header = document.querySelector('.header'); // dynamic header
+setHeader();
+
+// ----- check for Edit
+let id = localStorage.getItem('id');
+const sync = localStorage.getItem('sync') === 'true';
+const storageArea = !sync ? chrome.storage.local : chrome.storage.sync;
+if (id) { // This is an edit operation
+
+ storageArea.get(id, result => {
+
+ if (!Object.keys(result).length) {
+/*
+ if (id === LASTRESORT) { // error prevention
+ proxy = DEFAULT_PROXY_SETTING;
+ processOptions();
+ return;
+ }*/
+ console.error('Unable to edit saved proxy (could not get existing settings)')
+ return;
+ }
+ proxy = result[id];
+ processOptions();
+ });
+}
+
+
+// --- show & hide element using CSS
+const nav = [...document.querySelectorAll('input[name="nav"]')];
+//nav[0].checked = true;
+
+const proxyType = document.querySelector('#proxyType');
+proxyType.addEventListener('change', function() { nav[this.value -1].checked = true; });
+
+const proxyTitle = document.querySelector('#proxyTitle');
+proxyTitle.focus();
+
+const proxyAddress = document.querySelector('#proxyAddress');
+const proxyPort = document.querySelector('#proxyPort');
+const proxyUsername = document.querySelector('#proxyUsername');
+const proxyPassword = document.querySelector('#proxyPassword');
+const proxyActive = document.querySelector('#proxyActive');
+const proxyDNS = document.querySelector('#proxyDNS');
+const pacURL = document.querySelector('#pacURL');
+
+// --- remove nodes completely for FP Basic
+FOXYPROXY_BASIC && document.querySelectorAll('.notForBasic').forEach(item => item.remove());
+
+// --- remove pattern shortcuts if this is an edit operation
+id && document.querySelectorAll('.notForEdit').forEach(item => item.remove());
+
+// --- add Listeners
+document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));
+function process() {
+
+ switch (this.dataset.i18n) {
+
+ case 'cancel':
+ close();
+ break;
+
+ case 'saveAdd':
+ if (!validateInput()) { return; }
+ storageArea.set(makeProxy(), resetOptions);
+ break;
+
+ case 'saveEditPattern':
+ if (!validateInput()) { return; }
+ storageArea.set(makeProxy(), () => {
+ localStorage.setItem('id', id); // in case new proxy was added
+ proxyPassword.value = ''; // prevent Firefox's save password prompt
+ location.href = '/patterns.html';
+ });
+ break;
+
+ case 'save':
+ if (!validateInput()) { return; }
+ storageArea.set(makeProxy(), () => {
+ proxyPassword.value = ''; // prevent Firefox's save password promp
+ location.href = '/options.html'
+ });
+ break;
+
+ case 'togglePW|title':
+ const inp = this.nextElementSibling;
+ inp.type = inp.type === 'password' ? 'text' : 'password';
+ break;
+ }
+}
+
+function setHeader(proxy) {
+
+ if (proxy) {
+ document.title = 'FoxyProxy ' + chrome.i18n.getMessage('editProxy', '');
+ header.textContent = chrome.i18n.getMessage('editProxy', proxy.title || `${proxy.address}:${proxy.port}`);
+ return;
+ }
+ document.title = 'FoxyProxy ' + chrome.i18n.getMessage('addProxy');
+ header.textContent = chrome.i18n.getMessage('addProxy');
+}
+
+
+function processOptions() {
+
+ setHeader(proxy);
+
+ // select
+ proxyType.value = proxy.type;
+ nav[proxyType.value -1].checked = true;
+
+ // checkbox
+ proxyActive.checked = proxy.active;
+ proxyDNS.checked = proxy.proxyDNS || false;
+
+ // color
+ color.fromString(proxy.color || DEFAULT_COLOR);
+
+ // input
+ proxyTitle.value = proxy.title || '';
+ proxyAddress.value = proxy.address || '';
+ proxyPort.value = proxy.port || '';
+ proxyUsername.value = proxy.username || '';
+ proxyPassword.value = proxy.password || '';
+ pacURL.value = proxy.pacURL || '';
+}
+
+function makeProxy() {
+
+ proxy.type = proxyType.value *1;
+ proxy.color = document.querySelector('#colorChooser').value;
+ proxy.title = proxyTitle.value;
+ proxy.active = proxyActive.checked;
+
+ if (proxy.type !== PROXY_TYPE_NONE) {
+
+ proxy.address = proxyAddress.value;
+ proxy.port = proxyPort.value *1;
+ proxy.proxyDNS = proxy.type === PROXY_TYPE_SOCKS5 && proxyDNS.checked;
+ // already trimmed in validateInput()
+ proxy.username = proxyUsername.value; // if it had u/p and then deletd it, it must be reflected
+ proxy.password = proxyPassword.value;
+ }
+ if (FOXYPROXY_BASIC) {
+ proxy.whitePatterns = proxy.blackPatterns = [];
+ }
+ else {
+ proxy.whitePatterns = proxy.whitePatterns || (document.querySelector('#whiteAll').checked ? [PATTERN_ALL_WHITE] : []);
+ proxy.blackPatterns = proxy.blackPatterns || (document.querySelector('#blackAll').checked ? blacklistSet : []);
+ }
+ proxy.pacURL = proxy.pacURL || pacURL.value; // imported foxyproxy.xml
+
+ if (!id) { // global
+ // This is an add operation since id does not exist. If this is an edit op, then id is already set.
+ // Get the nextIndex given to us by options.js and subtract by the number of proxies we've added
+ // while this window has been open. This ensures this proxy setting is first in list of all proxy settings.
+ proxy.index = (localStorage.getItem('nextIndex')) - (++proxiesAdded);
+ id = Utils.getUniqueId();
+ }
+ // else proxy.index is already set for edit operations
+ return {[id]: proxy};
+}
+
+function validateInput() {
+
+ document.querySelectorAll('input[type="text"]').forEach(item => item.value = item.value.trim());
+
+ if (proxyType.value *1 === PROXY_TYPE_NONE) { return true; }
+
+ // let's handle here, #proxyPort will be checked later separately
+ // escape all inputs
+ [proxyTitle, proxyAddress].forEach(item => item.value = Utils.stripBadChars(item.value));
+
+ // checking proxyAddress
+ proxyAddress.classList.remove('invalid'); // reset
+ if (!proxyAddress.value) {
+ proxyAddress.classList.add('invalid');
+ return false;
+ }
+
+ // checking proxyPort
+ proxyPort.classList.remove('invalid'); // reset
+ if (!proxyPort.value *1) { // check to see if it is a digit and not 0
+ proxyPort.classList.add('invalid');
+ return false;
+ }
+
+ return true;
+}
+
+
+function resetOptions() {
+
+ localStorage.removeItem('id');
+ id = null;
+
+ // to help entering sets quickly, some fields are kept
+ [proxyTitle, proxyAddress].forEach(item => item.value = '');
+ color.fromString(DEFAULT_COLOR);
+
+ setHeader();
+ proxyTitle.focus();
+}
+
+function close() {
+ proxyPassword.value = ''; /* prevent Firefox's save password prompt */
+ location.href = '/options.html';
+} \ No newline at end of file
diff --git a/src/scripts/utils.js b/src/scripts/utils.js
new file mode 100644
index 0000000..e456d23
--- /dev/null
+++ b/src/scripts/utils.js
@@ -0,0 +1,324 @@
+'use strict';
+
+// ----------------- Constants -----------------------------
+const FOXYPROXY_BASIC = false;
+
+// Bit-wise flags so we can add/remove these independently. We may add more later so PROTOCOL_ALL is future-proof.
+const PROTOCOL_ALL = 1; // in case other protocols besides http and https are supported later
+const PROTOCOL_HTTP = 2;
+const PROTOCOL_HTTPS = 4;
+
+
+// import | pac
+const PROXY_TYPE_HTTP = 1;
+const PROXY_TYPE_HTTPS = 2;
+const PROXY_TYPE_SOCKS5 = 3;
+const PROXY_TYPE_SOCKS4 = 4;
+const PROXY_TYPE_NONE = 5; // DIRECT
+const PROXY_TYPE_PAC = 6;
+const PROXY_TYPE_WPAD = 7;
+const PROXY_TYPE_SYSTEM = 8;
+const PROXY_TYPE_PASS = 9;
+
+
+const PATTERN_TYPE_WILDCARD = 1;
+const PATTERN_TYPE_REGEXP = 2;
+
+// Storage keys that are not proxy settings
+const NON_PROXY_KEYS = ['mode', 'logging', 'sync', 'browserVersion', 'foxyProxyVersion', 'foxyProxyEdition', 'nextIndex'];
+
+// bg | import | proxy | utils
+const PATTERN_ALL_WHITE = {
+ title: 'all URLs',
+ active: true,
+ pattern: '*',
+ type: 1, // PATTERN_TYPE_WILDCARD,
+ protocols: 1 // PROTOCOL_ALL
+};
+
+const DEFAULT_COLOR = '#66cc66'; // default proxy color
+
+// patterns | proxy
+// the local-internal blacklist, always used as a set
+const blacklistSet = [
+ {
+ title: "local hostnames (usually no dots in the name). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
+ pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?(?:localhost|127\\.\\d+\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
+ },
+ {
+ title: "local subnets (IANA reserved address space). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
+ pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?(?:192\\.168\\.\\d+\\.\\d+|10\\.\\d+\\.\\d+\\.\\d+|172\\.(?:1[6789]|2[0-9]|3[01])\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
+ },
+ {
+ title: "localhost - matches the local host optionally prefixed by a user:password authentication string and optionally suffixed by a port number. The entire local subnet (127.0.0.0/8) matches. Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
+ pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?[\\w-]+(?::\\d+)?(?:/.*)?$"
+ }
+].map (item => {
+ item.active = true;
+ item.type = 2; // PATTERN_TYPE_REGEXP,
+ item.protocols = 1; // PROTOCOL_ALL
+ return item;
+});
+
+// ----------------- Utils ---------------------------------
+class Utils {
+
+ static notify(message, title = 'FoxyProxy') {
+ // the id is not used anywhere and can be omitted, it is only useful if you want to manually close the notification early
+ chrome.notifications.create('foxyproxy', {
+ type: 'basic',
+ iconUrl: '/images/icon.svg',
+ title,
+ message
+ });
+ }
+
+ // options | popup
+ static isUnsupportedType(type) {
+ //return type === PROXY_TYPE_PAC || type === PROXY_TYPE_WPAD || type === PROXY_TYPE_SYSTEM || type === PROXY_TYPE_PASS;
+ return [PROXY_TYPE_PAC, PROXY_TYPE_WPAD, PROXY_TYPE_SYSTEM, PROXY_TYPE_PASS].includes(type);
+ }
+
+ // bg | pattern-tester | validate-pattern
+ static wildcardToRegExp(pat) {
+
+ let start = 0, end = pat.length, matchOptionalSubdomains = false;
+
+ if (pat[0] === '.') { pat = '*' + pat; }
+
+ if (pat.startsWith('**')) {
+ // Strip asterisks from front and back
+ while (pat[start] === '*' && start < end) start++;
+ while (pat[end - 1] === '*' && start < end) end--;
+ // If there's only an asterisk left, match everything
+ if (end - start == 1 && pat[start] == '*') return '';
+ }
+ else if (pat.startsWith('*.')) { matchOptionalSubdomains = true; }
+
+ let regExpStr = pat.substring(start, end+1)
+ // $& replaces with the string found, but with that string escaped
+ .replace(/[$.+()^{}\]\[|]/g, '\\$&')
+ .replace(/\*/g, '.*')
+ .replace(/\?/g, '.');
+
+ if (matchOptionalSubdomains) {
+ // Non-capturing group that matches:
+ // any group of non-whitespace characters following by an optional . repeated zero or more times
+ regExpStr = '(?:\\S+\\.)*' + regExpStr.substring(4);
+ }
+
+ // Leading or ending double-asterisks mean exact starting and ending positions
+ if (start === 0) { regExpStr = '^' + regExpStr; }
+ if (end === pat.length) { regExpStr += '$'; }
+ return regExpStr;
+ }
+
+ // Prep the patternObject for matching: convert wildcards to regexp,
+ // store the originalPattern which the user entered so we can display if needed, etc.
+ // Return null if patternObject is inactive or there is an error.
+ static processPatternObject(patternObject) {
+ if (patternObject.active) {
+ // Store the original pattern so if this pattern matches something,
+ // we can display whatever the user entered ("original") in the log.
+ patternObject.originalPattern = patternObject.pattern;
+ if (patternObject.type === PATTERN_TYPE_WILDCARD) {
+ patternObject.pattern = Utils.wildcardToRegExp(patternObject.pattern);
+ }
+ try {
+ // Convert to real RegExp, not just a string. Validate. If invalid, notify user.
+ patternObject.pattern = new RegExp(patternObject.pattern, 'i');
+ return patternObject;
+ }
+ catch(e) {
+ console.error(`Error creating regexp for pattern: ${patternObject.pattern}`, e);
+ Utils.notify(`Error creating regular expression for pattern ${regExpStr}`);
+ }
+ }
+ return null;
+ }
+
+ // import | pattern
+ static importFile(file, mimeTypeArr, maxSizeBytes, jsonOrXml, callback) {
+
+ if (!file) {
+ alert('There was an error');
+ return;
+ }
+
+ // Check MIME type // Ch65 no filetype for JSON
+ if (!mimeTypeArr.includes(file.type)) {
+ alert('Unsupported file format');
+ return;
+ }
+
+ if (file.size > maxSizeBytes) {
+ alert('Filesize is too large');
+ return;
+ }
+
+ const reader = new FileReader();
+ reader.onloadend = () => {
+ if (reader.error) {
+ alert('Error reading file.');
+ return;
+ }
+
+ let settings;
+ try {
+ if (jsonOrXml === 'json') { settings = JSON.parse(reader.result); }
+ else if (jsonOrXml === 'xml') {
+ settings = new DOMParser().parseFromString(reader.result, 'text/xml');
+ if (settings.documentElement.nodeName === 'parsererror') { throw new Error(); }
+ }
+ }
+ catch(e) {
+ console.log(e);
+ alert("Error parsing file. Please remove sensitive data from the file, and then email it to support@getfoxyproxy.org so we can fix bugs in our parser.");
+ return;
+ }
+ if (settings && confirm('This will overwite existing proxy settings. Are you sure?')) { callback(settings); }
+ else { callback(); }
+
+ };
+ reader.onerror = () => { alert('Error reading file'); };
+ reader.readAsText(file);
+ }
+
+ // import | options
+ static exportFile() {
+
+ chrome.storage.local.get(null, result => {
+ browser.runtime.getBrowserInfo().then((bi) => {
+ !result.sync ? Utils.saveAs(result, bi.version) : chrome.storage.sync.get(null, result => {
+ Utils.saveAs(result, bi.version, true);
+ });
+ });
+ });
+ }
+ // exportFile helper
+ static saveAs(data, browserVersion, sync) {
+
+ const settings = data; //Utils.prepareForSettings(data);
+ // Browser version and extension version. These are used for debugging.
+ settings.browserVersion = browserVersion;
+ settings.foxyProxyVersion = chrome.runtime.getManifest().version;
+ settings.foxyProxyEdition = FOXYPROXY_BASIC ? 'basic' : 'standard';
+ settings.sync = sync;
+ const blob = new Blob([JSON.stringify(settings, null, 2)], {type : 'text/plain;charset=utf-8'});
+ const filename = chrome.i18n.getMessage('extensionName') + '_' + new Date().toISOString().substring(0, 10) + '.json';
+ chrome.downloads.download({
+ url: URL.createObjectURL(blob),
+ filename,
+ saveAs: true,
+ conflictAction: 'uniquify'
+ });
+ }
+
+ static updateIcon(iconPath, color, title, titleIsKey, badgeText, badgeTextIsKey) {
+ chrome.browserAction.setIcon({path: iconPath});
+ if (color) {
+ chrome.browserAction.setBadgeBackgroundColor({color: color});
+ }
+ else {
+ // TODO: confirm this is OK to do
+ chrome.browserAction.setBadgeBackgroundColor({color: null});
+ }
+ if (title) {
+ chrome.browserAction.setTitle({title: 'FoxyProxy: ' + (titleIsKey ? chrome.i18n.getMessage(title) : title)});
+ }
+ else {
+ chrome.browserAction.setTitle({title: ''});
+ }
+ if (badgeText) {
+ chrome.browserAction.setBadgeText({text: badgeTextIsKey ? chrome.i18n.getMessage(badgeText) : badgeText});
+ }
+ else {
+ chrome.browserAction.setBadgeText({text: ''});
+ }
+ }
+
+ static getProxyTitle(proxySetting) {
+ if (proxySetting.title) {
+ return proxySetting.title;
+ }
+ else if (proxySetting.type === PROXY_TYPE_NONE) {
+ return 'Direct (no proxy)';
+ }
+ else {
+ return `${proxySetting.address}:${proxySetting.port}`;
+ }
+ }
+
+/*
+ // utils only used for export, will be removed as DB format export is adapted
+ static prepareForSettings(settings = {}) {
+
+ //if (settings && !settings.mode) { }// 5.0 settings
+
+ let lastResortFound = false;
+ const prefKeys = Object.keys(settings);
+
+ const def = {
+ id: LASTRESORT,
+ active: true,
+ title: 'Default',
+ notes: 'These are the settings that are used when no patterns match a URL.',
+ color: '#0055E5',
+ type: PROXY_TYPE_NONE,
+ whitePatterns: [PATTERN_ALL_WHITE],
+ blackPatterns: []
+ };
+
+ // base format
+ const ret = {
+ mode: 'disabled',
+ proxySettings: [],
+ logging: {
+ size: 500,
+ active: true
+ }
+ };
+
+ if (!prefKeys.length) { // settings is {}
+ ret.proxySettings = [def];
+ return ret;
+ }
+
+ prefKeys.forEach(key => {
+
+ switch (key) {
+
+ case 'mode':
+ case 'logging':
+ ret[key] = settings[key];
+ break;
+
+ case 'sync': break; // do nothing
+
+ default:
+ const temp = settings[key];
+ temp.id = key; // Copy the id into the object
+ temp.id === LASTRESORT && (lastResortFound = true);
+ ret.proxySettings.push(temp);
+ }
+ });
+
+ ret.proxySettings.sort((a, b) => a.index - b.index);
+ ret.proxySettings.forEach(item => delete item.index); // Re-calculated when/if this object is written to disk again (user may move proxySetting up/down)
+
+ !lastResortFound && ret.proxySettings.push(def); // add default lastresort
+
+ return ret;
+ }
+*/
+
+ static getUniqueId() {
+ // We don't need cryptographically secure UUIDs, just something unique
+ return Math.random().toString(36).substring(7) + new Date().getTime();
+ }
+
+ static stripBadChars(str) {
+ return str ? str.replace(/[&<>"']+/g, '') : null;
+ }
+
+}