|
35 | 35 |
|
36 | 36 | var
|
37 | 37 | lodash = require('../lib_managed/lodash'),
|
| 38 | + cookie = require('browser-cookie-lite'), |
38 | 39 |
|
39 | 40 | object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
|
40 | 41 |
|
|
290 | 291 | }
|
291 | 292 | };
|
292 | 293 |
|
| 294 | + /** |
| 295 | + * Finds the root domain |
| 296 | + */ |
| 297 | + object.findRootDomain = function () { |
| 298 | + var cookiePrefix = '_sp_root_domain_test_'; |
| 299 | + var cookieName = cookiePrefix + new Date().getTime(); |
| 300 | + var cookieValue = '_test_value_' + new Date().getTime(); |
| 301 | + |
| 302 | + var split = window.location.hostname.split('.'); |
| 303 | + var position = split.length - 1; |
| 304 | + while (position >= 0) { |
| 305 | + var currentDomain = split.slice(position, split.length).join('.'); |
| 306 | + cookie.cookie(cookieName, cookieValue, 0, '/', currentDomain); |
| 307 | + if (cookie.cookie(cookieName) === cookieValue) { |
| 308 | + |
| 309 | + // Clean up created cookie(s) |
| 310 | + object.deleteCookie(cookieName, currentDomain); |
| 311 | + var cookieNames = object.getCookiesWithPrefix(cookiePrefix); |
| 312 | + for (var i = 0; i < cookieNames.length; i++) { |
| 313 | + object.deleteCookie(cookieNames[i], currentDomain); |
| 314 | + } |
| 315 | + |
| 316 | + return currentDomain; |
| 317 | + } |
| 318 | + position -= 1; |
| 319 | + } |
| 320 | + |
| 321 | + // Cookies cannot be read |
| 322 | + return window.location.hostname; |
| 323 | + }; |
| 324 | + |
| 325 | + /** |
| 326 | + * Checks whether a value is present within an array |
| 327 | + * |
| 328 | + * @param val The value to check for |
| 329 | + * @param array The array to check within |
| 330 | + * @return boolean Whether it exists |
| 331 | + */ |
| 332 | + object.isValueInArray = function (val, array) { |
| 333 | + for (var i = 0; i < array.length; i++) { |
| 334 | + if (array[i] === val) { |
| 335 | + return true; |
| 336 | + } |
| 337 | + } |
| 338 | + return false; |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Deletes an arbitrary cookie by setting the expiration date to the past |
| 343 | + * |
| 344 | + * @param cookieName The name of the cookie to delete |
| 345 | + * @param domainName The domain the cookie is in |
| 346 | + */ |
| 347 | + object.deleteCookie = function (cookieName, domainName) { |
| 348 | + cookie.cookie(cookieName, '', -1, '/', domainName); |
| 349 | + }; |
| 350 | + |
| 351 | + /** |
| 352 | + * Fetches the name of all cookies beginning with a certain prefix |
| 353 | + * |
| 354 | + * @param cookiePrefix The prefix to check for |
| 355 | + * @return array The cookies that begin with the prefix |
| 356 | + */ |
| 357 | + object.getCookiesWithPrefix = function (cookiePrefix) { |
| 358 | + var cookies = document.cookie.split("; "); |
| 359 | + var cookieNames = []; |
| 360 | + for (var i = 0; i < cookies.length; i++) { |
| 361 | + if (cookies[i].startsWith(cookiePrefix)) { |
| 362 | + cookieNames.push(cookies[i]); |
| 363 | + } |
| 364 | + } |
| 365 | + return cookieNames; |
| 366 | + }; |
| 367 | + |
| 368 | + /** |
| 369 | + * Parses an object and returns either the |
| 370 | + * integer or undefined. |
| 371 | + * |
| 372 | + * @param obj The object to parse |
| 373 | + * @return the result of the parse operation |
| 374 | + */ |
| 375 | + object.parseInt = function (obj) { |
| 376 | + var result = parseInt(obj); |
| 377 | + return isNaN(result) ? undefined : result; |
| 378 | + } |
| 379 | + |
| 380 | + /** |
| 381 | + * Parses an object and returns either the |
| 382 | + * number or undefined. |
| 383 | + * |
| 384 | + * @param obj The object to parse |
| 385 | + * @return the result of the parse operation |
| 386 | + */ |
| 387 | + object.parseFloat = function (obj) { |
| 388 | + var result = parseFloat(obj); |
| 389 | + return isNaN(result) ? undefined : result; |
| 390 | + } |
| 391 | + |
293 | 392 | }());
|
0 commit comments