From 4d93227b9ca9003c3b26c14c305f62265a38d6a8 Mon Sep 17 00:00:00 2001 From: u3u Date: Fri, 3 Aug 2018 18:27:37 +0800 Subject: [PATCH] fix: unable to display image in new version of chrome (close #19) --- src/index.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 0d19556..c4bbf90 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -console.img = function(url) { +console.img = function(url, ...args) { if (!/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(url)) { console.warn('Image URL is incorrect') return @@ -14,16 +14,27 @@ console.img = function(url) { }) } - img.onload = evt => { + img.addEventListener('load', evt => { const image = evt.target - const width = (arguments[1] || image.width) / 2 - const height = (arguments[2] || image.height) / 2 + const width = (args[0] || image.width) / 2 + const height = (args[1] || image.height) / 2 const lineHeight = height * 2 + + const [, version] = navigator.userAgent.match(/Chrome\/([\d|.]+) /) || [] + const needLineHeight = version ? version < '68.0.3440.84' : true + console.log( - '%c', - `font-size: 0; padding: ${height}px ${width}px; line-height: ${lineHeight}px; background: url(${url}) no-repeat 50% / 100%` + '%c ', + [ + 'font-size: 0', + `padding: ${height}px ${width}px`, + needLineHeight && `line-height: ${lineHeight}px`, + `background: url(${url}) no-repeat 50% / 100%` + ] + .filter(Boolean) + .join('; ') ) - } + }) img.src = url return promise