Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: files/parse.js

Issue 8493027: Acquired Opera AdBlock code (Closed)
Patch Set: Created Oct. 8, 2012, 5:58 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « files/lists.js ('k') | files/preferences.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: files/parse.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/files/parse.js
@@ -0,0 +1,106 @@
+const parse = {
+ adblock: function(content, add) {
+ var url = this.wildcard(content.replace(/^@@/, '').replace(/\$.*$/, ''));
+ var option = undefined;
+ if(content.match(/\$/)) { //Is a type-specific rule
+ option = this.options(content.split('$')[1].split(','));
+ }
+
+ if(content.match(/^@@/)) {
+ if(add)
+ opera.extension.urlfilter.allow.add(url, option);
+ else
+ opera.extension.urlfilter.allow.remove(url, option);
+ }
+ else {
+ if(add)
+ opera.extension.urlfilter.block.add(url, option);
+ else
+ opera.extension.urlfilter.block.remove(url, option);
+ }
+ },
+ adblockOld: function(content, add) { //Parsing of the content for older versions of Opera
+ if(!content.match(/^@@/)) { //Whitelist is not supported
+ var url = this.sepCharacters([this.wildcard(content.replace(/\$.*$/, ''))]); //Removes the special rules as those are not supported on older versions of Opera
+ if(add)
+ for(i in url)
+ opera.extension.urlfilter.block.add(url[i]);
+ else
+ for(i in url)
+ opera.extension.urlfilter.block.remove(url[i]);
+ }
+ },
+ map: {
+ 'script': opera.extension.urlfilter.RESOURCE_SCRIPT,
+ 'image': opera.extension.urlfilter.RESOURCE_IMAGE,
+ 'stylesheet': opera.extension.urlfilter.RESOURCE_STYLESHEET,
+ 'object': opera.extension.urlfilter.RESOURCE_OBJECT,
+ 'xmlhttprequest': opera.extension.urlfilter.RESOURCE_XMLHTTPREQUEST,
+ 'object-subrequest': opera.extension.urlfilter.RESOURCE_OBJECT_SUBREQUEST,
+ 'subdocument': opera.extension.urlfilter.RESOURCE_SUBDOCUMENT,
+ 'document': opera.extension.urlfilter.RESOURCE_DOCUMENT,
+ //'refresh': opera.extension.urlfilter.RESOURCE_REFRESH,
+ //'media': opera.extension.urlfilter.RESOURCE_MEDIA,
+ //'font': opera.extension.urlfilter.RESOURCE_FONT,
+ 'other': opera.extension.urlfilter.RESOURCE_OTHER,
+ 'all': opera.extension.urlfilter.RESOURCE_SCRIPT | opera.extension.urlfilter.RESOURCE_IMAGE | opera.extension.urlfilter.RESOURCE_STYLESHEET | opera.extension.urlfilter.RESOURCE_OBJECT |
+ opera.extension.urlfilter.RESOURCE_XMLHTTPREQUEST | opera.extension.urlfilter.RESOURCE_OBJECT_SUBREQUEST | opera.extension.urlfilter.RESOURCE_SUBDOCUMENT | opera.extension.urlfilter.RESOURCE_DOCUMENT |
+ opera.extension.urlfilter.RESOURCE_REFRESH | opera.extension.urlfilter.RESOURCE_MEDIA | opera.extension.urlfilter.RESOURCE_FONT | opera.extension.urlfilter.RESOURCE_OTHER
+ },
+ options: function(special) {
+ var option = {excludeDomains: [], includeDomains: [], resources: this.map['all']};
+ var additive = false;
+ for(i in special) {
+ var element = special[i];
+ if(element.match(/^domain/i) == 0) { //TESTED
+ var domains = special[i].split('=')[1].split(',');
+ for(j in domains) {
+ if(domains[j].match(/^~/)) //Don't apply on this website
+ option['excludeDomains'].push(domains[j]);
+ else //Apply on this website
+ option['includeDomains'].push(domains[j]);
+ }
+ }
+ else if(!element.match(/^elemhide/i)) { //No support for ElemHide - TESTED
+ if(!element.match(/^~/i)) {
+ if(!additive) {
+ additive = true;
+ option['resources'] = 0;
+ }
+ option['resources'] += this.map[element];
+ }
+ else if(!additive)
+ option['resources'] -= this.map[element];
+ }
+ }
+ return option;
+ },
+ wildcard: function(url) { //TESTED
+ //| at the beginning or end means no wildcard
+ if(!url.match(/^\|\|/)) { //Does not starts with ||
+ if(url.match(/^\|/)) //Starts with |
+ url = url.replace(/^\|/, ''); //Remove the |
+ else if(!url.match(/^\*/)) //Avoid **
+ url = '*' + url; //Add the wildcard
+ }
+ if(url.match(/\|$/)) //Ends with |
+ url = url.replace(/\|$/, ''); //Remove the |
+ else if(!url.match(/\*$/)) //Avoid **
+ url = url + '*'; //Add the wildcard
+
+ return url;
+ },
+ sepCharacters: function(value) { //Separator charactor not supported in <12.1
+ //var characters = '(){}[],;:!?/|\\"\'#$&*+<=>@^`~'; //WARNING: Using the longer list WILL freeze the browser in some instances
+ var characters = ':?/#&='; //Shorter string to avoid over-processing
+ var values = new Array();
+ for(i in value) {
+ for(j in characters) {
+ values.push(value[i].replace(/\^/, characters[j]));
+ }
+ if(values[0].match(/\^/))
+ return this.sepCharacters(values);
+ }
+ return values;
+ }
+};
« no previous file with comments | « files/lists.js ('k') | files/preferences.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld