Skip to content

Commit 5c2825a

Browse files
fix: Remove style attribute from body for prevent unexpected background
1 parent 1c61a0c commit 5c2825a

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/common/src/createQRScannerAdapter.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = function createQRScanner(cordova){
2+
var initialStyles = { ...document.body.style };
3+
var backgroundTransparent = true;
4+
var backgroundTimeouts = [];
5+
26
// The native implementations should return their status as ['string':'string']
37
// dictionaries. Boolean values are encoded to '0' and '1', respectively.
48
function stringToBool(string) {
@@ -30,6 +34,21 @@ function convertStatus(statusDictionary) {
3034
};
3135
}
3236

37+
// Reset body style attribute and clear pending timeouts for omit unexpected style changes.
38+
function resetBodyStyles() {
39+
setTimeout(function() {
40+
backgroundTransparent = false;
41+
for (var backgroundTimeout of backgroundTimeouts) {
42+
if (backgroundTimeout) {
43+
clearTimeout(backgroundTimeout);
44+
}
45+
}
46+
for (var key of Object.keys(document.body.style)) {
47+
document.body.style[key] = initialStyles[key] ?? '';
48+
}
49+
}, 100);
50+
}
51+
3352
// Simple utility method to ensure the background is transparent. Used by the
3453
// plugin to force re-rendering immediately after the native webview background
3554
// is made transparent.
@@ -38,9 +57,11 @@ function clearBackground() {
3857
if (body.style) {
3958
body.style.backgroundColor = 'rgba(0,0,0,0.01)';
4059
body.style.backgroundImage = '';
41-
setTimeout(function() {
42-
body.style.backgroundColor = 'transparent';
43-
}, 1);
60+
if (backgroundTransparent) {
61+
backgroundTimeouts.push(setTimeout(function () {
62+
body.style.backgroundColor = 'transparent';
63+
}, 1));
64+
}
4465
if (body.parentNode && body.parentNode.style) {
4566
body.parentNode.style.backgroundColor = 'transparent';
4667
body.parentNode.style.backgroundImage = '';
@@ -155,10 +176,12 @@ function doneCallback(callback, clear) {
155176

156177
return {
157178
prepare: function(callback) {
179+
backgroundTransparent = true;
158180
cordova.exec(successCallback(callback), errorCallback(callback), 'QRScanner', 'prepare', []);
159181
},
160182
destroy: function(callback) {
161183
cordova.exec(doneCallback(callback, true), null, 'QRScanner', 'destroy', []);
184+
resetBodyStyles();
162185
},
163186
scan: function(callback) {
164187
if (!callback) {

www/www.min.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ cordovaModule.exports = QRScannerAdapter;
103103
/***/ (function(module, exports) {
104104

105105
module.exports = function createQRScanner(cordova){
106+
var initialStyles = {...document.body.style};
107+
var backgroundTransparent = true;
108+
var backgroundTimeouts = [];
109+
106110
// The native implementations should return their status as ['string':'string']
107111
// dictionaries. Boolean values are encoded to '0' and '1', respectively.
108112
function stringToBool(string) {
@@ -134,6 +138,21 @@ function convertStatus(statusDictionary) {
134138
};
135139
}
136140

141+
// Reset body style attribute and clear pending timeouts for omit unexpected style changes.
142+
function resetBodyStyles() {
143+
setTimeout(function() {
144+
backgroundTransparent = false;
145+
for (var backgroundTimeout of backgroundTimeouts) {
146+
if (backgroundTimeout) {
147+
clearTimeout(backgroundTimeout);
148+
}
149+
}
150+
for (var key of Object.keys(document.body.style)) {
151+
document.body.style[key] = initialStyles[key] ?? '';
152+
}
153+
}, 100);
154+
}
155+
137156
// Simple utility method to ensure the background is transparent. Used by the
138157
// plugin to force re-rendering immediately after the native webview background
139158
// is made transparent.
@@ -142,16 +161,17 @@ function clearBackground() {
142161
if (body.style) {
143162
body.style.backgroundColor = 'rgba(0,0,0,0.01)';
144163
body.style.backgroundImage = '';
145-
setTimeout(function() {
146-
body.style.backgroundColor = 'transparent';
147-
}, 1);
164+
if (backgroundTransparent) {
165+
backgroundTimeouts.push(setTimeout(function () {
166+
body.style.backgroundColor = 'transparent';
167+
}, 1));
168+
}
148169
if (body.parentNode && body.parentNode.style) {
149170
body.parentNode.style.backgroundColor = 'transparent';
150171
body.parentNode.style.backgroundImage = '';
151172
}
152173
}
153174
}
154-
155175
function errorCallback(callback) {
156176
if (!callback) {
157177
return null;
@@ -259,10 +279,12 @@ function doneCallback(callback, clear) {
259279

260280
return {
261281
prepare: function(callback) {
282+
backgroundTransparent = true;
262283
cordova.exec(successCallback(callback), errorCallback(callback), 'QRScanner', 'prepare', []);
263284
},
264285
destroy: function(callback) {
265286
cordova.exec(doneCallback(callback, true), null, 'QRScanner', 'destroy', []);
287+
resetBodyStyles();
266288
},
267289
scan: function(callback) {
268290
if (!callback) {
@@ -339,4 +361,4 @@ module.exports = cordova;
339361

340362
/***/ })
341363

342-
/******/ });
364+
/******/ });

0 commit comments

Comments
 (0)