Skip to content

Commit 1baa60d

Browse files
fix: Remove style attribute from body for prevent unexpected background
1 parent b62efb6 commit 1baa60d

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
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] ? 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) {

0 commit comments

Comments
 (0)