Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove style attribute from body for prevent unexpected background #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] ? 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