Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Split the data URI only once.
Browse files Browse the repository at this point in the history
Probably not a significant performance improvement considering how JavaScript splits strings internally, but it's also slightly more readable this way.
  • Loading branch information
ericlaw1979 committed Aug 4, 2019
1 parent 0b66308 commit 563a2e0
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions extension/background.js
Expand Up @@ -37,18 +37,19 @@ const API_URL = goog.define('API_URL', 'https://test-safebrowsing.google.com');
* @return {!Uint8Array} A Uin8Array representation of the image.
*/
const getIntArrayFromDataUrl = dataUrl => {
const urlParts = dataUrl.split(',');
// Separate out the mime component, which comes after the data: prefix and
// before ;base64. e.g., image/png for screenshot data.
const mimeString =
dataUrl.split(',')[0].split(':')[1].split(';')[0];
urlParts[0].split(':')[1].split(';')[0];
// Since the data URL is retrieved from chrome.tabs.captureVisibleTab with
// the format set to png, verify that the mimeString matches.
if (mimeString !== 'image/png') {
throw Error('Screenshot is in incorrect format');
}
// Convert base 64 to raw binary data held in a string. The data segment
// comes after the comma in a data URL.
const byteString = atob(dataUrl.split(',')[1]);
const byteString = atob(urlParts[1]);
// Write the bytes of the string to an ArrayBuffer.
const intArray =
new Uint8Array(new ArrayBuffer(byteString.length));
Expand Down

0 comments on commit 563a2e0

Please sign in to comment.