From 07ba0a0a4bcf234ef2ba4140522054f407c24095 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 18 Jun 2015 08:27:44 -0400 Subject: [PATCH] this fixes #270 --- src/js/user-rules.js | 56 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/js/user-rules.js b/src/js/user-rules.js index 53ea13a..e1476c6 100644 --- a/src/js/user-rules.js +++ b/src/js/user-rules.js @@ -95,12 +95,13 @@ var processUserRules = function(response) { /******************************************************************************/ +// https://github.com/chrisaljoudi/uBlock/issues/757 +// Support RequestPolicy rule syntax + var fromRequestPolicy = function(content) { - // https://github.com/chrisaljoudi/uBlock/issues/757 - // Support RequestPolicy rule syntax var matches = /\[origins-to-destinations\]([^\[]+)/.exec(content); if ( matches === null || matches.length !== 2 ) { - return content; + return ''; } return matches[1].trim() .replace(/\|/g, ' ') @@ -109,12 +110,61 @@ var fromRequestPolicy = function(content) { /******************************************************************************/ +// https://github.com/gorhill/uMatrix/issues/270 + +var fromNoScript = function(content) { + var noscript = null; + try { + noscript = JSON.parse(content); + } catch (e) { + } + if ( + noscript === null || + typeof noscript !== 'object' || + typeof noscript.prefs !== 'object' || + typeof noscript.prefs.clearClick === undefined || + typeof noscript.whitelist !== 'string' || + typeof noscript.V !== 'string' + ) { + return ''; + } + var out = {}; + var reBad = /[a-z]+:\w*$/; + var reURL = /[a-z]+:\/\/([0-9a-z.-]+)/; + var directives = noscript.whitelist.split(/\s+/); + var i = directives.length; + var directive, matches; + while ( i-- ) { + directive = directives[i].trim(); + if ( directive === '' ) { + continue; + } + if ( reBad.test(directive) ) { + continue; + } + matches = reURL.exec(directive); + if ( matches !== null ) { + directive = matches[1]; + } + out['* ' + directive + ' script allow'] = true; + } + return Object.keys(out).join('\n'); +}; + +/******************************************************************************/ + function handleImportFilePicker() { var fileReaderOnLoadHandler = function() { if ( typeof this.result !== 'string' || this.result === '' ) { return; } var result = fromRequestPolicy(this.result); + if ( result === '' ) { + result = fromNoScript(this.result); + if ( result === '' ) { + result = this.result; + } + } var request = { 'what': 'setUserRules', 'temporaryRules': rulesFromHTML('#diff .right li') + '\n' + result