-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnapsite.js
More file actions
68 lines (55 loc) · 1.54 KB
/
snapsite.js
File metadata and controls
68 lines (55 loc) · 1.54 KB
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
/*
SNAPSITE LIBRARY
VERSION 0.1
*/
"use strict";
var SnapSite = function (callback, options, error) {
//Library/Extension status
var isStarted = false;
var hasExtension = false;
//Custom options
options = options || {};
//Use your own html selector
options.selector = options.selector || "feedback";
//Start only if extension is installed
options.withExtension = options.withExtension || false;
//Listen response extension
window.addEventListener('message', function(event) {
if (event.source != window) return;
//Check availability of the extension
if (event.data.__SNAPSITE_AVAILABLE &&
event.data.__SNAPSITE_AVAILABLE == true) {
hasExtension = true;
start();
}
//Check if message is sent through the extension
if (event.data.snap) {
//Take snap
callback(event.data);
}
}, false);
//Private
function start() {
//Extension is required
//Library won't be launched until browser client install extension
if (options.withExtension === true && !hasExtension) return;
if (isStarted === false) {
//Ready to catch event
var catcher = document.getElementById(options.selector);
catcher.addEventListener('click', function() {
if (hasExtension === true) {
//Dispatch event
//Will be catched by extension
var event = new Event('snap');
document.dispatchEvent(event);
} else {
//Extension is not ready, install extension first.
error({"no-extension":"Please install extension first."});
}
}, false);
//Library is started
isStarted = true;
}
}
start();
}