forked from js-cookie/js-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
85 lines (81 loc) · 2.37 KB
/
utils.js
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
/* global Cookies, QUnit */
/* eslint-disable no-var */
;(function () {
window.lifecycle = {
afterEach: function () {
// Remove the cookies created using js-cookie default attributes
Object.keys(Cookies.get()).forEach(function (cookie) {
Cookies.remove(cookie)
})
// Remove the cookies created using browser default attributes
Object.keys(Cookies.get()).forEach(function (cookie) {
Cookies.remove(cookie, {
path: ''
})
})
}
}
window.using = function (assert) {
function getQuery (key) {
var queries = window.location.href.split('?')[1]
if (!queries) {
return
}
var pairs = queries.split(/&|=/)
var indexBaseURL = pairs.indexOf(key)
var result = pairs[indexBaseURL + 1]
if (result) {
return decodeURIComponent(result)
}
}
function setCookie (name, value) {
return {
then: function (callback) {
var iframe = document.getElementById('request_target')
var serverURL = getQuery('integration_baseurl')
Cookies.set(name, value)
if (!serverURL) {
callback(Cookies.get(name), document.cookie)
} else {
var requestURL = [
serverURL,
'encoding?',
'name=' + encodeURIComponent(name),
'&value=' + encodeURIComponent(value)
].join('')
var done = assert.async()
iframe.addEventListener('load', function () {
var iframeDocument = iframe.contentWindow.document
var root = iframeDocument.documentElement
var content = root.textContent
if (!content) {
QUnit.ok(
false,
['"' + requestURL + '"', 'content should not be empty'].join(
' '
)
)
done()
return
}
try {
var result = JSON.parse(content)
callback(result.value, iframeDocument.cookie)
} finally {
done()
}
})
iframe.src = requestURL
}
}
}
}
return {
setCookie: setCookie
}
}
window.quoted = function (input) {
return '"' + input + '"'
}
})()
/* eslint-enable no-var */