tarteaucitron.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*jslint browser: true */
  2. // define correct path for files inclusion
  3. var scripts = document.getElementsByTagName('script'),
  4. path = scripts[scripts.length - 1].src.split('?')[0],
  5. cdn = path.split('/').slice(0, -1).join('/') + '/';
  6. var tarteaucitron = {
  7. "autoOpen": false, // auto open the panel with #tarteaucitron hash ?
  8. "promoteThisScript": false, // promote this script if ads are denied ?
  9. "cdn": cdn,
  10. "user": {},
  11. "lang": {},
  12. "services": {},
  13. "state": [],
  14. "launch": [],
  15. "init": function () {
  16. "use strict";
  17. var cdn = tarteaucitron.cdn,
  18. language = tarteaucitron.getLanguage(),
  19. pathToLang = cdn + 'lang/tarteaucitron.' + language + '.js',
  20. pathToServices = cdn + 'tarteaucitron.services.js',
  21. linkElement = document.createElement('link');
  22. // Step 1: load css
  23. linkElement.rel = 'stylesheet';
  24. linkElement.type = 'text/css';
  25. linkElement.href = cdn + 'css/tarteaucitron.css';
  26. document.getElementsByTagName('head')[0].appendChild(linkElement);
  27. // Step 2: load language and services
  28. tarteaucitron.addScript(pathToLang, '', function () {
  29. tarteaucitron.addScript(pathToServices, '', function () {
  30. var body = document.body,
  31. div = document.createElement('div'),
  32. hostname = document.location.hostname,
  33. hostRef = document.referrer.split('/')[2],
  34. isNavigating = (hostRef === hostname) ? true : false,
  35. isAutostart,
  36. isDenied,
  37. isAllowed,
  38. isResponded,
  39. cookie = tarteaucitron.cookie.read(),
  40. s = tarteaucitron.services,
  41. service,
  42. html = '',
  43. lastTitle,
  44. alert = false,
  45. index;
  46. // dedup, clean and sort job[]
  47. function cleanArray(arr) {
  48. var i,
  49. len = arr.length,
  50. out = [],
  51. obj = {};
  52. for (i = 0; i < len; i += 1) {
  53. if (!obj[arr[i]]) {
  54. obj[arr[i]] = {};
  55. if (tarteaucitron.services[arr[i]] !== undefined) {
  56. out.push(arr[i]);
  57. }
  58. }
  59. }
  60. return out;
  61. }
  62. tarteaucitron.job = cleanArray(tarteaucitron.job);
  63. tarteaucitron.job = tarteaucitron.job.sort(function (a, b) {
  64. if (s[a].type + s[a].key > s[b].type + s[b].key) { return 1; }
  65. if (s[a].type + s[a].key < s[b].type + s[b].key) { return -1; }
  66. return 0;
  67. });
  68. // if bypass: load all services and exit
  69. // for example, set tarteaucitron.user.bypass = true;
  70. // if the user is not in europa
  71. if (tarteaucitron.user.bypass === true) {
  72. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  73. service = s[tarteaucitron.job[index]];
  74. service.js();
  75. }
  76. return;
  77. }
  78. // Step 3: prepare the html
  79. html += '<div id="tarteaucitronBack"></div>';
  80. html += '<div id="tarteaucitron">';
  81. html += ' <div id="tarteaucitronClosePanel" onclick="tarteaucitron.userInterface.closePanel();">';
  82. html += ' ' + tarteaucitron.lang.close;
  83. html += ' </div>';
  84. html += ' <div id="tarteaucitronDisclaimer">';
  85. html += ' ' + tarteaucitron.lang.disclaimer;
  86. html += ' </div>';
  87. html += ' <div id="tarteaucitronServices">';
  88. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  89. service = s[tarteaucitron.job[index]];
  90. if (lastTitle !== service.type) {
  91. html += '<div class="tarteaucitronTitle">';
  92. html += ' ' + tarteaucitron.lang[service.type].title;
  93. html += ' <div class="tarteaucitronDetails">';
  94. html += ' ' + tarteaucitron.lang[service.type].details;
  95. html += ' </div>';
  96. html += '</div>';
  97. lastTitle = service.type;
  98. }
  99. html += '<div id="' + service.key + 'Line" class="tarteaucitronLine">';
  100. html += ' <div class="tarteaucitronName">';
  101. html += ' <b>' + service.name + '</b><br/>';
  102. html += ' <a href="' + service.uri + '" target="_blank">';
  103. html += ' ' + tarteaucitron.lang.more + ' : ' + service.uri.split('/')[2];
  104. html += ' </a>';
  105. html += ' </div>';
  106. html += ' <div class="tarteaucitronAsk">';
  107. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  108. html += ' ' + tarteaucitron.lang.allow;
  109. html += ' </div> ';
  110. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  111. html += ' ' + tarteaucitron.lang.deny;
  112. html += ' </div>';
  113. html += ' </div>';
  114. html += '</div>';
  115. html += '<div class="clear"></div>';
  116. }
  117. html += ' </div>';
  118. html += '</div>';
  119. html += '<div id="tarteaucitronAlertBig">';
  120. html += ' <span id="tarteaucitronDisclaimerAlert">';
  121. html += ' ' + tarteaucitron.lang.alertBig;
  122. html += ' </span>';
  123. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.openPanel();">';
  124. html += ' ' + tarteaucitron.lang.personalize;
  125. html += ' </span>';
  126. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.closeAlert();">';
  127. html += ' ' + tarteaucitron.lang.close;
  128. html += ' </span>';
  129. html += '</div>';
  130. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  131. html += ' <span id="tarteaucitronDot"></span>';
  132. html += ' ' + tarteaucitron.lang.alertSmall;
  133. html += '</div>';
  134. div.id = 'tarteaucitronRoot';
  135. body.appendChild(div, body);
  136. div.innerHTML = html;
  137. // Step 4: load services
  138. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  139. service = s[tarteaucitron.job[index]];
  140. isAutostart = (!service.needConsent) ? true : false;
  141. isDenied = (cookie.indexOf(service.key + '=false') >= 0) ? true : false;
  142. isAllowed = (cookie.indexOf(service.key + '=true') >= 0) ? true : false;
  143. isResponded = (cookie.indexOf(service.key) >= 0) ? true : false;
  144. if ((!isResponded && (isAutostart || isNavigating)) || isAllowed) {
  145. if (!isAllowed) {
  146. tarteaucitron.cookie.create(service.key, true);
  147. }
  148. if (tarteaucitron.launch[service.key] !== true) {
  149. tarteaucitron.launch[service.key] = true;
  150. service.js();
  151. }
  152. tarteaucitron.state[service.key] = true;
  153. tarteaucitron.userInterface.color(service.key, true);
  154. } else if (isDenied) {
  155. if (typeof service.fallback === 'function') {
  156. service.fallback();
  157. }
  158. tarteaucitron.state[service.key] = false;
  159. tarteaucitron.userInterface.color(service.key, false);
  160. } else if (!isResponded) {
  161. if (typeof service.fallback === 'function') {
  162. service.fallback();
  163. }
  164. }
  165. if (tarteaucitron.state[service.key] === undefined && !alert) {
  166. alert = true;
  167. }
  168. }
  169. // Step 5: display the alert
  170. if (alert) {
  171. tarteaucitron.userInterface.openAlert();
  172. } else {
  173. tarteaucitron.userInterface.closeAlert();
  174. }
  175. if (document.location.hash === '#tarteaucitron' && tarteaucitron.autoOpen === true) {
  176. tarteaucitron.userInterface.openPanel();
  177. }
  178. });
  179. });
  180. },
  181. "userInterface": {
  182. "css": function (id, property, value) {
  183. "use strict";
  184. document.getElementById(id).style[property] = value;
  185. },
  186. "respond": function (el, status) {
  187. "use strict";
  188. var key = el.id.replace(new RegExp("(Allow|Deni)ed", "g"), '');
  189. // return if same state
  190. if (tarteaucitron.state[key] === status) {
  191. return;
  192. }
  193. // if not already launched... launch the service
  194. if (status === true) {
  195. if (tarteaucitron.launch[key] !== true) {
  196. tarteaucitron.launch[key] = true;
  197. tarteaucitron.services[key].js();
  198. }
  199. }
  200. tarteaucitron.state[key] = status;
  201. tarteaucitron.cookie.create(key, status);
  202. tarteaucitron.userInterface.color(key, status);
  203. },
  204. "color": function (key, status) {
  205. "use strict";
  206. var gray = '#808080',
  207. greenDark = '#1B870B',
  208. greenLight = '#E6FFE2',
  209. redDark = '#9C1A1A',
  210. redLight = '#FFE2E2',
  211. c = 'tarteaucitron',
  212. allAllowed = true,
  213. index;
  214. if (status === true) {
  215. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', greenLight);
  216. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  217. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  218. } else if (status === false) {
  219. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', redLight);
  220. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  221. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  222. }
  223. // check if all services are allowed
  224. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  225. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  226. allAllowed = false;
  227. break;
  228. }
  229. }
  230. if (allAllowed) {
  231. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', greenDark);
  232. } else {
  233. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', redDark);
  234. }
  235. },
  236. "openPanel": function () {
  237. "use strict";
  238. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  239. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  240. tarteaucitron.userInterface.closeAlert();
  241. // setting hash tag
  242. document.location.hash = 'tarteaucitron';
  243. },
  244. "closePanel": function () {
  245. "use strict";
  246. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  247. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  248. },
  249. "openAlert": function () {
  250. "use strict";
  251. var c = 'tarteaucitron';
  252. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  253. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  254. },
  255. "closeAlert": function () {
  256. "use strict";
  257. var c = 'tarteaucitron';
  258. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  259. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  260. }
  261. },
  262. "cookie": {
  263. "create": function (key, status) {
  264. "use strict";
  265. var d = new Date(),
  266. time = d.getTime(),
  267. expireTime = time + 31536000000, // 365 days
  268. regex = new RegExp("!" + key + "=(true|false)", "g"),
  269. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  270. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  271. d.setTime(expireTime);
  272. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  273. },
  274. "read": function () {
  275. "use strict";
  276. var nameEQ = "tarteaucitron=",
  277. ca = document.cookie.split(';'),
  278. i,
  279. c;
  280. for (i = 0; i < ca.length; i += 1) {
  281. c = ca[i];
  282. while (c.charAt(0) === ' ') {
  283. c = c.substring(1, c.length);
  284. }
  285. if (c.indexOf(nameEQ) === 0) {
  286. return c.substring(nameEQ.length, c.length);
  287. }
  288. }
  289. return '';
  290. }
  291. },
  292. "getLanguage": function () {
  293. "use strict";
  294. if (!navigator) { return 'en'; }
  295. var availableLanguages = 'en,fr',
  296. defaultLanguage = 'en',
  297. lang = navigator.language || navigator.browserLanguage ||
  298. navigator.systemLanguage || navigator.userLang || null,
  299. userLanguage = lang.substr(0, 2);
  300. if (availableLanguages.indexOf(userLanguage) === -1) {
  301. return defaultLanguage;
  302. }
  303. return userLanguage;
  304. },
  305. "addScript": function (url, id, callback) {
  306. "use strict";
  307. var script = document.createElement('script'),
  308. done = false;
  309. script.type = 'text/javascript';
  310. script.id = (id !== undefined) ? id : '';
  311. script.async = true;
  312. script.src = url;
  313. if (typeof callback === 'function') {
  314. script.onreadystatechange = script.onload = function () {
  315. var state = script.readyState;
  316. if (!done && (!state || /loaded|complete/.test(state))) {
  317. done = true;
  318. callback();
  319. }
  320. };
  321. }
  322. document.getElementsByTagName('head')[0].appendChild(script);
  323. },
  324. "fallback": function (matchClass, content) {
  325. "use strict";
  326. var elems = document.getElementsByTagName('*'),
  327. i,
  328. index = 0;
  329. for (i in elems) {
  330. if (elems[i] !== undefined) {
  331. for (index = 0; index < matchClass.length; index += 1) {
  332. if ((' ' + elems[i].className + ' ')
  333. .indexOf(' ' + matchClass[index] + ' ') > -1) {
  334. if (typeof content === 'function') {
  335. elems[i].innerHTML = content(elems[i]);
  336. } else {
  337. elems[i].innerHTML = content;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. },
  344. /***
  345. * Fallback function for advertising services
  346. *
  347. * Currently, a banner to promote tarteaucitron.js is displayed,
  348. * fell free to change this by your own ads.
  349. *
  350. * Because eval() id devil, you can't pass <script>,
  351. * only html like <a><img /></a>
  352. */
  353. "promoteMe": function (el) {
  354. "use strict";
  355. if (tarteaucitron.promoteThisScript === false) { return ''; }
  356. var l = tarteaucitron.getLanguage(),
  357. w = el.offsetWidth,
  358. h = el.offsetHeight,
  359. s = '',
  360. r = '';
  361. if (w > 0 && h === 0) {
  362. h = 60;
  363. }
  364. if (w >= 728 && h <= 60) {
  365. h = 90;
  366. }
  367. if (w >= 970 && h >= 250) {
  368. s = '970250';
  369. } else if (w >= 970 && h >= 90) {
  370. s = '97090';
  371. } else if (w >= 728 && h >= 90) {
  372. s = '72890';
  373. } else if (w >= 468 && h >= 60) {
  374. s = '46860';
  375. } else if (w >= 336 && h >= 280) {
  376. s = '336280';
  377. } else if (w >= 300 && h >= 600) {
  378. s = '300600';
  379. } else if (w >= 300 && h >= 250) {
  380. s = '300250';
  381. } else if (w >= 250 && h >= 250) {
  382. s = '250250';
  383. } else if (w >= 200 && h >= 200) {
  384. s = '200200';
  385. } else if (w >= 160 && h >= 600) {
  386. s = '160600';
  387. } else if (w >= 120 && h >= 600) {
  388. s = '120600';
  389. } else if (w >= 120 && h >= 300) {
  390. s = '120300';
  391. } else if (w >= 120 && h >= 240) {
  392. s = '120240';
  393. } else {
  394. s = '8080';
  395. }
  396. r += '<a href="//opt-out.ferank.eu/" target="_blank" rel="nofollow">';
  397. r += ' <img src="//opt-out.ferank.eu/b/' + l + '/' + s + '.gif" border="0" />';
  398. r += '</a>';
  399. return r;
  400. }
  401. };