|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const DROPBOX_RECEIVER_HTML = '/dropbox-oauth.html'; |
| 4 | +const DROPBOX_API_KEY = 'uyfixgzre8v1bkg'; |
| 5 | +const FILENAME = 'stylus.json'; |
| 6 | +const API_ERROR_STATUS_FILE_NOT_FOUND = 409; |
| 7 | +const HTTP_STATUS_CANCEL = 499; |
| 8 | + |
| 9 | +/** |
| 10 | + * this was the only way that worked in keeping a value from page to page with location.href (oauth return) |
| 11 | + * tried localStorage, but didn't work :/ |
| 12 | + */ |
| 13 | +function hasDropboxAccessToken() { |
| 14 | + if (typeof browser !== 'undefined') { /* firefox */ |
| 15 | + return browser.storage.local.get('dropbox_access_token') |
| 16 | + .then(item => { |
| 17 | + return item.dropbox_access_token; |
| 18 | + }); |
| 19 | + } else { /* chrome */ |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + chrome.storage.local.get(['dropbox_access_token'], result => { |
| 22 | + resolve(result.dropbox_access_token); |
| 23 | + }); |
| 24 | + }); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function openDropboxOauthPage() { |
| 29 | + let client = new Dropbox.Dropbox({clientId: DROPBOX_API_KEY}); |
| 30 | + let authUrl = client.getAuthenticationUrl(window.location.origin + DROPBOX_RECEIVER_HTML); |
| 31 | + |
| 32 | + window.location.href = authUrl; |
| 33 | +} |
| 34 | + |
| 35 | +function uploadFileDropbox(client, stylesText) { |
| 36 | + return client.filesUpload({path: '/' + FILENAME, contents: stylesText}); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +$('#sync-dropbox-export').onclick = async () => { |
| 41 | + let accessToken = await hasDropboxAccessToken(); |
| 42 | + if (!accessToken) { |
| 43 | + openDropboxOauthPage(); |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + let client = new Dropbox.Dropbox({ |
| 49 | + clientId: DROPBOX_API_KEY, |
| 50 | + accessToken: accessToken |
| 51 | + }); |
| 52 | + |
| 53 | + /** |
| 54 | + * check if the file exists, if exists, delete it before upload another |
| 55 | + */ |
| 56 | + client.filesDownload({path: '/' + FILENAME}) |
| 57 | + .then(responseGet => { |
| 58 | + /** deletes file if user want to */ |
| 59 | + if (!confirm(t('overwriteFileExport'))) { |
| 60 | + return Promise.reject({status: HTTP_STATUS_CANCEL}); |
| 61 | + } |
| 62 | + |
| 63 | + return client.filesDelete({path: '/' + FILENAME}); |
| 64 | + }) |
| 65 | + .then(responseDelete => { |
| 66 | + /** file deleted with success, get styles and create a file */ |
| 67 | + return API.getStyles().then(styles => JSON.stringify(styles, null, '\t')) |
| 68 | + }) |
| 69 | + .then(stylesText => { |
| 70 | + /** create file dropbox */ |
| 71 | + return uploadFileDropbox(client, stylesText); |
| 72 | + }) |
| 73 | + .then(responseSave => { |
| 74 | + alert(t('exportSavedSuccess')); |
| 75 | + }) |
| 76 | + .catch(async error => { |
| 77 | + /* saving file first time */ |
| 78 | + if (error.status === API_ERROR_STATUS_FILE_NOT_FOUND) { |
| 79 | + let stylesText = await API.getStyles().then(styles => JSON.stringify(styles, null, '\t')); |
| 80 | + |
| 81 | + uploadFileDropbox(client, stylesText) |
| 82 | + .then(response => { |
| 83 | + alert(t('exportSavedSuccess')); |
| 84 | + }) |
| 85 | + .catch(err => { |
| 86 | + console.error(error); |
| 87 | + }); |
| 88 | + |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + /* user cancelled the flow */ |
| 93 | + if (error.status === HTTP_STATUS_CANCEL) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + console.error(error); |
| 98 | + |
| 99 | + return; |
| 100 | + }); |
| 101 | + |
| 102 | +}; |
| 103 | + |
| 104 | +$('#sync-dropbox-import').onclick = async () => { |
| 105 | + |
| 106 | + let accessToken = await hasDropboxAccessToken(); |
| 107 | + if (!accessToken) { |
| 108 | + openDropboxOauthPage(); |
| 109 | + |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + let client = new Dropbox.Dropbox({ |
| 114 | + clientId: DROPBOX_API_KEY, |
| 115 | + accessToken: accessToken |
| 116 | + }); |
| 117 | + |
| 118 | + client.filesDownload({path: '/' + FILENAME}) |
| 119 | + .then(response => { |
| 120 | + let fileBlob = response.fileBlob; |
| 121 | + |
| 122 | + /* it's based on the import-export.js */ |
| 123 | + const fReader = new FileReader(); |
| 124 | + fReader.onloadend = event => { |
| 125 | + const text = event.target.result; |
| 126 | + const maybeUsercss = !/^[\s\r\n]*\[/.test(text) && |
| 127 | + (text.includes('==UserStyle==') || /==UserStyle==/i.test(text)); |
| 128 | + |
| 129 | + (!maybeUsercss ? |
| 130 | + importFromString(text) : |
| 131 | + getOwnTab().then(tab => { |
| 132 | + tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'})); |
| 133 | + return API.installUsercss({direct: true, tab}) |
| 134 | + .then(() => URL.revokeObjectURL(tab.url)); |
| 135 | + }) |
| 136 | + ); |
| 137 | + }; |
| 138 | + fReader.readAsText(fileBlob, 'utf-8'); |
| 139 | + }) |
| 140 | + .catch(error => { |
| 141 | + /* no file */ |
| 142 | + if (error.status === API_ERROR_STATUS_FILE_NOT_FOUND) { |
| 143 | + alert(t('noFileToImport')); |
| 144 | + |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + console.error(err); |
| 149 | + }); |
| 150 | + |
| 151 | + return; |
| 152 | +}; |
0 commit comments