Skip to content

Commit e167c40

Browse files
erisuOS-kepatotorica
authored andcommitted
fix(statusbar): inject script block to compute color & replace padStart w/ logic supported on SDK 24 (apache#1853)
1 parent cef7ce9 commit e167c40

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cordova-js-src/plugin/android/statusbar.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var exec = require('cordova/exec');
2323
var statusBarVisible = true;
2424
var statusBar = {};
2525

26+
// This <script> element is explicitly used by Cordova's statusbar for computing color. (Do not use this element)
27+
const statusBarScript = document.createElement('script');
28+
document.head.appendChild(statusBarScript);
29+
2630
Object.defineProperty(statusBar, 'visible', {
2731
configurable: false,
2832
enumerable: true,
@@ -54,9 +58,8 @@ Object.defineProperty(statusBar, 'setBackgroundColor', {
5458
enumerable: false,
5559
writable: false,
5660
value: function (value) {
57-
var script = document.querySelector('script[src*="cordova.js"]');
58-
script.style.color = value;
59-
var rgbStr = window.getComputedStyle(script).getPropertyValue('color');
61+
statusBarScript.style.color = value;
62+
var rgbStr = window.getComputedStyle(statusBarScript).getPropertyValue('color');
6063

6164
if (!rgbStr.match(/^rgb/)) { return; }
6265

@@ -68,7 +71,11 @@ Object.defineProperty(statusBar, 'setBackgroundColor', {
6871
rgbVals = [255].concat(rgbVals);
6972
}
7073

71-
const padRgb = (val) => val.toString(16).padStart(2, '0');
74+
// TODO: Use `padStart(2, '0')` once SDK 24 is dropped.
75+
const padRgb = (val) => {
76+
const hex = val.toString(16);
77+
return hex.length === 1 ? '0' + hex : hex;
78+
};
7279
const a = padRgb(rgbVals[0]);
7380
const r = padRgb(rgbVals[1]);
7481
const g = padRgb(rgbVals[2]);

0 commit comments

Comments
 (0)