tarteaucitron.js 19 KB

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