Skip to content

Commit

Permalink
fix: Remove style attribute from body for prevent unexpected background
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonso-salces committed Aug 18, 2022
1 parent b62efb6 commit c5b2b13
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/common/src/createQRScannerAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = function createQRScanner(cordova){
var initialStyles = { ...document.body.style };
var backgroundTransparent = true;
var backgroundTimeouts = [];

// The native implementations should return their status as ['string':'string']
// dictionaries. Boolean values are encoded to '0' and '1', respectively.
function stringToBool(string) {
Expand Down Expand Up @@ -30,6 +34,21 @@ function convertStatus(statusDictionary) {
};
}

// Reset body style attribute and clear pending timeouts for omit unexpected style changes.
function resetBodyStyles() {
setTimeout(function() {
backgroundTransparent = false;
for (var backgroundTimeout of backgroundTimeouts) {
if (backgroundTimeout) {
clearTimeout(backgroundTimeout);
}
}
for (var key of Object.keys(document.body.style)) {
document.body.style[key] = initialStyles[key] ?? '';
}
}, 100);
}

// Simple utility method to ensure the background is transparent. Used by the
// plugin to force re-rendering immediately after the native webview background
// is made transparent.
Expand All @@ -38,9 +57,11 @@ function clearBackground() {
if (body.style) {
body.style.backgroundColor = 'rgba(0,0,0,0.01)';
body.style.backgroundImage = '';
setTimeout(function() {
body.style.backgroundColor = 'transparent';
}, 1);
if (backgroundTransparent) {
backgroundTimeouts.push(setTimeout(function () {
body.style.backgroundColor = 'transparent';
}, 1));
}
if (body.parentNode && body.parentNode.style) {
body.parentNode.style.backgroundColor = 'transparent';
body.parentNode.style.backgroundImage = '';
Expand Down Expand Up @@ -155,10 +176,12 @@ function doneCallback(callback, clear) {

return {
prepare: function(callback) {
backgroundTransparent = true;
cordova.exec(successCallback(callback), errorCallback(callback), 'QRScanner', 'prepare', []);
},
destroy: function(callback) {
cordova.exec(doneCallback(callback, true), null, 'QRScanner', 'destroy', []);
resetBodyStyles();
},
scan: function(callback) {
if (!callback) {
Expand Down

0 comments on commit c5b2b13

Please sign in to comment.