|
| 1 | +/* global messageBox Dropbox createZipFileFromText readZipFileFromBlob launchWebAuthFlow getRedirectUrlAuthFlow importFromString resolve */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const DROPBOX_API_KEY = 'zg52vphuapvpng9'; |
| 5 | +const FILENAME_ZIP_FILE = 'stylus.json'; |
| 6 | +const DROPBOX_FILE = 'stylus.zip'; |
| 7 | +const API_ERROR_STATUS_FILE_NOT_FOUND = 409; |
| 8 | +const HTTP_STATUS_CANCEL = 499; |
| 9 | + |
| 10 | +function messageProgressBar(data) { |
| 11 | + return messageBox({ |
| 12 | + title: `${data.title}`, |
| 13 | + className: 'config-dialog', |
| 14 | + contents: [ |
| 15 | + $create('p', data.text) |
| 16 | + ], |
| 17 | + buttons: [{ |
| 18 | + textContent: t('confirmClose'), |
| 19 | + dataset: {cmd: 'close'}, |
| 20 | + }], |
| 21 | + }).then(() => { |
| 22 | + document.body.style.minWidth = ''; |
| 23 | + document.body.style.minHeight = ''; |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function hasDropboxAccessToken() { |
| 28 | + return chromeLocal.getValue('dropbox_access_token'); |
| 29 | +} |
| 30 | + |
| 31 | +function requestDropboxAccessToken() { |
| 32 | + const client = new Dropbox.Dropbox({clientId: DROPBOX_API_KEY}); |
| 33 | + const authUrl = client.getAuthenticationUrl(getRedirectUrlAuthFlow()); |
| 34 | + return launchWebAuthFlow({url: authUrl, interactive: true}) |
| 35 | + .then(urlReturned => { |
| 36 | + const params = new URLSearchParams(new URL(urlReturned).hash.replace('#', '')); |
| 37 | + chromeLocal.setValue('dropbox_access_token', params.get('access_token')); |
| 38 | + return params.get('access_token'); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function uploadFileDropbox(client, stylesText) { |
| 43 | + return client.filesUpload({path: '/' + DROPBOX_FILE, contents: stylesText}); |
| 44 | +} |
| 45 | + |
| 46 | +$('#sync-dropbox-export').onclick = () => { |
| 47 | + const title = t('syncDropboxStyles'); |
| 48 | + messageProgressBar({title: title, text: t('connectingDropbox')}); |
| 49 | + |
| 50 | + hasDropboxAccessToken() |
| 51 | + .then(token => token || requestDropboxAccessToken()) |
| 52 | + .then(token => { |
| 53 | + const client = new Dropbox.Dropbox({ |
| 54 | + clientId: DROPBOX_API_KEY, |
| 55 | + accessToken: token |
| 56 | + }); |
| 57 | + return client.filesDownload({path: '/' + DROPBOX_FILE}) |
| 58 | + .then(() => messageBox.confirm(t('overwriteFileExport'))) |
| 59 | + .then(ok => { |
| 60 | + // deletes file if user want to |
| 61 | + if (!ok) { |
| 62 | + return Promise.reject({status: HTTP_STATUS_CANCEL}); |
| 63 | + } |
| 64 | + return client.filesDelete({path: '/' + DROPBOX_FILE}); |
| 65 | + }) |
| 66 | + // file deleted with success, get styles and create a file |
| 67 | + .then(() => { |
| 68 | + messageProgressBar({title: title, text: t('gettingStyles')}); |
| 69 | + return API.getStyles().then(styles => JSON.stringify(styles, null, '\t')); |
| 70 | + }) |
| 71 | + // create zip file |
| 72 | + .then(stylesText => { |
| 73 | + messageProgressBar({title: title, text: t('zipStyles')}); |
| 74 | + return createZipFileFromText(FILENAME_ZIP_FILE, stylesText); |
| 75 | + }) |
| 76 | + // create file dropbox |
| 77 | + .then(zipedText => { |
| 78 | + messageProgressBar({title: title, text: t('uploadingFile')}); |
| 79 | + return uploadFileDropbox(client, zipedText); |
| 80 | + }) |
| 81 | + // gives feedback to user |
| 82 | + .then(() => messageProgressBar({title: title, text: t('exportSavedSuccess')})) |
| 83 | + // handle not found cases and cancel action |
| 84 | + .catch(error => { |
| 85 | + console.log(error); |
| 86 | + // saving file first time |
| 87 | + if (error.status === API_ERROR_STATUS_FILE_NOT_FOUND) { |
| 88 | + API.getStyles() |
| 89 | + .then(styles => { |
| 90 | + messageProgressBar({title: title, text: t('gettingStyles')}); |
| 91 | + return JSON.stringify(styles, null, '\t'); |
| 92 | + }) |
| 93 | + .then(stylesText => { |
| 94 | + messageProgressBar({title: title, text: t('zipStyles')}); |
| 95 | + return createZipFileFromText(FILENAME_ZIP_FILE, stylesText); |
| 96 | + }) |
| 97 | + .then(zipedText => { |
| 98 | + messageProgressBar({title: title, text: t('uploadingFile')}); |
| 99 | + return uploadFileDropbox(client, zipedText); |
| 100 | + }) |
| 101 | + .then(() => messageProgressBar({title: title, text: t('exportSavedSuccess')})) |
| 102 | + .catch(err => messageBox.alert(err)); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // user cancelled the flow |
| 107 | + if (error.status === HTTP_STATUS_CANCEL) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + console.error(error); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}; |
| 115 | + |
| 116 | +$('#sync-dropbox-import').onclick = () => { |
| 117 | + const title = t('retrieveDropboxSync'); |
| 118 | + messageProgressBar({title: title, text: t('connectingDropbox')}); |
| 119 | + |
| 120 | + hasDropboxAccessToken() |
| 121 | + .then(token => token || requestDropboxAccessToken()) |
| 122 | + .then(token => { |
| 123 | + const client = new Dropbox.Dropbox({ |
| 124 | + clientId: DROPBOX_API_KEY, |
| 125 | + accessToken: token |
| 126 | + }); |
| 127 | + return client.filesDownload({path: '/' + DROPBOX_FILE}) |
| 128 | + .then(response => { |
| 129 | + messageProgressBar({title: title, text: t('unzipStyles')}); |
| 130 | + return readZipFileFromBlob(response.fileBlob); |
| 131 | + }) |
| 132 | + .then(zipedFileBlob => { |
| 133 | + messageProgressBar({title: title, text: t('readingStyles')}); |
| 134 | + document.body.style.cursor = 'wait'; |
| 135 | + const fReader = new FileReader(); |
| 136 | + fReader.onloadend = event => { |
| 137 | + const text = event.target.result; |
| 138 | + const maybeUsercss = !/^[\s\r\n]*\[/.test(text) && |
| 139 | + (text.includes('==UserStyle==') || /==UserStyle==/i.test(text)); |
| 140 | + (!maybeUsercss ? |
| 141 | + importFromString(text) : |
| 142 | + getOwnTab().then(tab => { |
| 143 | + tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'})); |
| 144 | + return API.installUsercss({direct: true, tab}) |
| 145 | + .then(() => URL.revokeObjectURL(tab.url)); |
| 146 | + }) |
| 147 | + ).then(numStyles => { |
| 148 | + document.body.style.cursor = ''; |
| 149 | + resolve(numStyles); |
| 150 | + }); |
| 151 | + }; |
| 152 | + fReader.readAsText(zipedFileBlob, 'utf-8'); |
| 153 | + }) |
| 154 | + .catch(error => { |
| 155 | + // no file |
| 156 | + if (error.status === API_ERROR_STATUS_FILE_NOT_FOUND) { |
| 157 | + messageBox.alert(t('noFileToImport')); |
| 158 | + return; |
| 159 | + } |
| 160 | + messageBox.alert(error); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}; |
0 commit comments