Last active
November 22, 2017 23:49
-
-
Save mjbogusz/cb62ca0f840b3d00108ffa1be76d7ead to your computer and use it in GitHub Desktop.
Import/export tiles for WebExtension version of NewTabTools. Paste into console in new tab, then execute `exportTiles()` and `importTiles()`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // blob to base64: | |
| // https://stackoverflow.com/questions/12786818/file-api-blob-to-json | |
| // https://stackoverflow.com/a/11562550/3217805 | |
| // base64 to blob: | |
| // https://stackoverflow.com/a/16245768/3217805 | |
| var exportTiles = function() { | |
| Tiles.getAllTiles().then(tiles => { | |
| return Promise.all(tiles.map(tile => { | |
| return new Promise(resolve => { | |
| let fr = new FileReader(); | |
| fr.onload = function (event) { | |
| tile.imageString = btoa(String.fromCharCode(...new Uint8Array(event.target.result))); | |
| resolve(tile); | |
| }; | |
| if (tile.image) { | |
| fr.readAsArrayBuffer(tile.image); | |
| } else { | |
| resolve(tile); | |
| } | |
| }); | |
| })); | |
| }).then(tiles => { | |
| let exportDiv = document.createElement('div'); | |
| exportDiv.style.cssText = 'position: absolute; width: 100%; height: 100%; z-index: 100; opacity: 0.9; background: #fff; color: #000;'; | |
| let exportDivButton = document.createElement('input'); | |
| exportDivButton.type = 'button'; | |
| exportDivButton.value = 'close'; | |
| exportDivButton.onclick = () => { | |
| document.body.removeChild(exportDiv); | |
| } | |
| exportDiv.appendChild(exportDivButton); | |
| let exportDivText = document.createElement('p'); | |
| exportDivText.textContent = JSON.stringify(tiles); | |
| exportDiv.appendChild(exportDivText); | |
| document.body.appendChild(exportDiv); | |
| }); | |
| }; | |
| var importTiles = function() { | |
| let importDiv = document.createElement('div'); | |
| importDiv.style.cssText = 'position: absolute; width: 100%; height: 100%; z-index: 100; opacity: 0.9; background: #fff; color: #000;'; | |
| let importDivInput = document.createElement('textarea'); | |
| importDivInput.style.cssText = 'width: 90%; height: 80%;'; | |
| let importDivButton = document.createElement('input'); | |
| importDivButton.type = 'button'; | |
| importDivButton.value = 'import'; | |
| importDivButton.onclick = () => { | |
| let tiles; | |
| try { | |
| tiles = JSON.parse(importDivInput.value); | |
| } catch (error) { | |
| window.alert('bad JSON format'); | |
| return; | |
| } | |
| console.log('PARSED:', tiles); | |
| return Promise.all(tiles.map(tile => { | |
| try { | |
| if (tile.imageString) { | |
| const byteCharacters = atob(tile.imageString); | |
| const byteArrays = []; | |
| for (let offset = 0; offset < byteCharacters.length; offset += 512) { | |
| const slice = byteCharacters.slice(offset, offset + 512); | |
| const byteNumbers = new Array(slice.length); | |
| for (let i = 0; i < slice.length; i++) { | |
| byteNumbers[i] = slice.charCodeAt(i); | |
| } | |
| byteArrays.push(new Uint8Array(byteNumbers)); | |
| } | |
| tile.image = new Blob(byteArrays, { type: "image/png" }); | |
| } | |
| } catch (error) { | |
| tile.imageString = ''; | |
| window.alert('error converting tile image: ' + JSON.stringify(tile) + '\nError: ' + error); | |
| return; | |
| } | |
| return Tiles.putTile(tile).catch(error => { | |
| tile.imageString = tile.imageString ? '' : tile.imageString; | |
| window.alert('error importing tile: ' + JSON.stringify(tile) + '\nError: ' + error); | |
| }); | |
| })).then(() => { | |
| window.alert('Import OK!'); | |
| }); | |
| } | |
| let importDivCloseButton = document.createElement('input'); | |
| importDivCloseButton.type = 'button'; | |
| importDivCloseButton.value = 'close'; | |
| importDivCloseButton.onclick = () => { | |
| document.body.removeChild(importDiv); | |
| } | |
| importDiv.appendChild(importDivButton); | |
| importDiv.appendChild(importDivCloseButton); | |
| importDiv.appendChild(document.createElement('br')); | |
| importDiv.appendChild(importDivInput); | |
| document.body.appendChild(importDiv); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment