summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/url_handler/url_handler.js
blob: 41ec82e6ac22da0c0e3b7a963443fc8ff510e2aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
 * *
 * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see  <http://www.gnu.org/licenses/>.
 *
 */

/**
 *  url_handler
 *  A module using the url tool from Node.js to perform operations on
 *  urls at various spots (tabs, caching, ...) in the add-on.
 *
 */

// node.js url module. Makes it easier to resole 
// urls in that datauri loaded dom
var nodeJsUrl = require("url_handler/node_url");

var urlUtils = {

  getFragment: function (url) {
	  var parse =  nodeJsUrl.parse(url);
	  if (parse.hash !== undefined) {
	    return(parse.hash);
	  }
  },

  removeFragment: function (url) {
	  var parse =  nodeJsUrl.parse(url);
	  if (parse.hash !== undefined) {
	    // Amazon track package bug fix.
	    // when url has query string and fragment
	    // the add-on wouldn't remove cache entry
	    // properly.
	    delete parse.hash;
	  }
	  return nodeJsUrl.format(parse);
  },

  addFragment: function (url, query) {
	  var parse = nodeJsUrl.parse(url);

	  // replace hash if it exists.
	  parse.hash = '#' + query;

	  return nodeJsUrl.format(parse);
  },

  addQuery: function (url, query) {
	  var parse = nodeJsUrl.parse(url);
	  console.debug('my parse search', parse.search);
	  if (parse.search === undefined) {
	    parse.search = '?' + query;
	  } else {
	    parse.search = parse.search + '&' + query;
	    console.debug('parse search is now' + parse.search);
	  }
	  return nodeJsUrl.format(parse);
  },

  getHostname: function (url) {
	  return nodeJsUrl.parse(url).hostname;
  },

  /**
   * remove www from hostname.
   */
  removeWWW: function (str) {
    if (str !== undefined) {
	    return str.replace("www.", "", 'i');
    }
    return "";
  },

  /**
   *
   * haveSameHostname
   * Compare that two urls have the same hostname.
   *
   */
  haveSameHostname: function (url1, url2) {

	  try {

	    var host1 = this.removeWWW(this.getHostname(url1)).toLowerCase();
	    var host2 = this.removeWWW(this.getHostname(url2)).toLowerCase();
	    return host1 === host2;

	  } catch (x) {

	    console.debug('error with url_handler', x, x.fileName, x.lineNumber);
      
	  }
  }
};

exports.parse = nodeJsUrl.parse;
exports.resolve = nodeJsUrl.resolve;
exports.resolveObject = nodeJsUrl.resolveObject;
exports.format = nodeJsUrl.format;
exports.removeFragment = urlUtils.removeFragment;
exports.addQuery = urlUtils.addQuery;
exports.getFragment = urlUtils.getFragment;
exports.addFragment = urlUtils.addFragment;
exports.getHostname = urlUtils.getHostname;
exports.haveSameHostname = urlUtils.haveSameHostname;
exports.removeWWW = urlUtils.removeWWW;