|
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 | + * Deletes an arbitrary cookie by setting the expiration date to the past |
| 327 | + * |
| 328 | + * @param cookieName The name of the cookie to delete |
| 329 | + * @param domainName The domain the cookie is in |
| 330 | + */ |
| 331 | + object.deleteCookie = function (cookieName, domainName) { |
| 332 | + cookie.cookie(cookieName, '', -1, '/', domainName); |
| 333 | + }; |
| 334 | + |
| 335 | + /** |
| 336 | + * Fetches the name of all cookies beginning with a certain prefix |
| 337 | + * |
| 338 | + * @param cookiePrefix The prefix to check for |
| 339 | + * @return array The cookies that begin with the prefix |
| 340 | + */ |
| 341 | + object.getCookiesWithPrefix = function (cookiePrefix) { |
| 342 | + var cookies = document.cookie.split("; "); |
| 343 | + var cookieNames = []; |
| 344 | + for (var i = 0; i < cookies.length; i++) { |
| 345 | + if (cookies[i].startsWith(cookiePrefix)) { |
| 346 | + cookieNames.push(cookies[i]); |
| 347 | + } |
| 348 | + } |
| 349 | + return cookieNames; |
| 350 | + }; |
| 351 | + |
293 | 352 | }()); |
0 commit comments