-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromeStrap.js
152 lines (127 loc) · 3.87 KB
/
ChromeStrap.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var $popup = '<div id="bs-bp-notify" style="display: none;">'+
'<span class="bs-bp-dismiss">'+
'<a title="Dismiss">x</a>'+
'<div class="bs-bp-message">message</div>'+
'</span>'+
'</div>';
var timeout;
var options;
var defaultSites = new Array();
defaultSites[0] = "http://.*";
chrome.storage.sync.get({
enabled: 'true',
popupenabled: 'true',
showtype: 0,
sites: defaultSites
}, function(items) {
options = items;
initChromeStrap();
});
function reloadOptions() {
chrome.storage.sync.get({
enabled: 'true',
popupenabled: 'true',
showtype: 0,
sites: defaultSites
}, function(items) {
options = items;
});
}
function initChromeStrap() {
if(timeout) clearTimeout(timeout);
if(!options || !options.enabled || !siteMatch(options.showtype, options.sites)) {
return;
}
$('body').append($popup);
$(window).resize(runBootstrapCheck);
$(".bs-bp-dismiss a").click(function() {
$("#bs-bp-notify").stop().fadeOut("fast");
});
$(".bs-bp-dismiss").hover(
function() {
clearTimeout(timeout);
$('#bs-bp-notify').stop().fadeIn("fast");
},
function() {
timeout = setTimeout(function() { $('#bs-bp-notify').stop().fadeOut("fast"); }, 1000);
}
);
}
function runBootstrapCheck() {
if(!options || !options.enabled || !siteMatch(options.showtype, options.sites)) {
return;
}
var spec = viewport();
var bp = findBootstrapEnvironment();
var range = getBootstrapRange(bp);
clearTimeout(timeout);
if(options.popupenabled) {
var message = '<div><strong>W:</strong> '+spec.width+'px</div>'+
'<div><strong>H:</strong> '+spec.height+'px</div>'+
'<div><strong>Break:</strong> '+bp+'</div>';
if(range!=null) {
message = message+'<div><strong>Range<a class="info" title="Assuming default Bootstrap settings">?</a>:</strong> '+range.min+'px - '+range.max+'px</div>';
}
$('#bs-bp-notify .bs-bp-message').html(message);
$('#bs-bp-notify').stop().fadeIn("fast");
timeout = setTimeout(function() { $('#bs-bp-notify').stop().fadeOut("fast"); }, 2000);
}
if(bp.length <= 4) {
chrome.extension.sendMessage(bp);
} else {
chrome.extension.sendMessage("");
}
}
function viewport() {
var e = window, a = 'inner';
if (!('innerWidth' in window )) {
a = 'client';
e = document.documentElement || document.body;
}
return { width : e [ a+'Width' ], height : e [ a+'Height' ] };
}
function findBootstrapEnvironment() {
var envs = ["xs", "sm", "md", "lg"];
var doc = window.document;
var temp = doc.createElement("div");
doc.body.appendChild(temp);
for (var i = envs.length - 1; i >= 0; i--) {
var env = envs[i];
temp.className = "hidden-" + env;
if (temp.offsetParent === null) {
doc.body.removeChild(temp);
return env;
}
}
doc.body.removeChild(temp);
return "Bootstrap not in use";
}
function getBootstrapRange(bp) {
switch(bp) {
case "xs":
return { min: 1, max: 767 };
case "sm":
return { min: 768, max: 991 };
case "md":
return { min: 992, max: 1199 };
case "lg":
return { min: 1200, max: screen.width };
}
}
function siteMatch(showType, sites) {
if(showType == 0) {
for (i = 0; i < sites.length; ++i) {
if(document.URL.match(sites[i])) {
return true;
}
}
return false;
} else {
for (i = 0; i < sites.length; ++i) {
if(document.URL.match(sites[i])) {
return false;
}
}
return true;
}
}