fileSaveLoad.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* global messageBox, handleUpdate, applyOnMessage */
  2. 'use strict';
  3. const STYLISH_DUMP_FILE_EXT = '.txt';
  4. const STYLUS_BACKUP_FILE_EXT = '.json';
  5. function importFromFile({fileTypeFilter, file} = {}) {
  6. return new Promise(resolve => {
  7. const fileInput = document.createElement('input');
  8. if (file) {
  9. readFile();
  10. return;
  11. }
  12. fileInput.style.display = 'none';
  13. fileInput.type = 'file';
  14. fileInput.accept = fileTypeFilter || STYLISH_DUMP_FILE_EXT;
  15. fileInput.acceptCharset = 'utf-8';
  16. document.body.appendChild(fileInput);
  17. fileInput.initialValue = fileInput.value;
  18. fileInput.onchange = readFile;
  19. fileInput.click();
  20. function readFile() {
  21. if (file || fileInput.value !== fileInput.initialValue) {
  22. file = file || fileInput.files[0];
  23. if (file.size > 100e6) {
  24. console.warn("100MB backup? I don't believe you.");
  25. importFromString('').then(resolve);
  26. return;
  27. }
  28. document.body.style.cursor = 'wait';
  29. const fReader = new FileReader();
  30. fReader.onloadend = event => {
  31. fileInput.remove();
  32. importFromString(event.target.result).then(numStyles => {
  33. document.body.style.cursor = '';
  34. resolve(numStyles);
  35. });
  36. };
  37. fReader.readAsText(file, 'utf-8');
  38. }
  39. }
  40. });
  41. }
  42. function importFromString(jsonString) {
  43. if (!BG) {
  44. onBackgroundReady().then(() => importFromString(jsonString));
  45. return;
  46. }
  47. // create objects in background context
  48. const json = BG.tryJSONparse(jsonString) || [];
  49. if (typeof json.slice !== 'function') {
  50. json.length = 0;
  51. }
  52. const oldStyles = json.length && BG.deepCopy(BG.cachedStyles.list || []);
  53. const oldStylesByName = json.length && new Map(
  54. oldStyles.map(style => [style.name.trim(), style]));
  55. const stats = {
  56. added: {names: [], ids: [], legend: 'importReportLegendAdded'},
  57. unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'},
  58. metaAndCode: {names: [], ids: [], legend: 'importReportLegendUpdatedBoth'},
  59. metaOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedMeta'},
  60. codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'},
  61. invalid: {names: [], legend: 'importReportLegendInvalid'},
  62. };
  63. let index = 0;
  64. let lastRenderTime = performance.now();
  65. const renderQueue = [];
  66. const RENDER_NAP_TIME_MAX = 1000; // ms
  67. const RENDER_QUEUE_MAX = 50; // number of styles
  68. const SAVE_OPTIONS = {reason: 'import', notify: false};
  69. return new Promise(proceed);
  70. function proceed(resolve) {
  71. while (index < json.length) {
  72. const item = json[index++];
  73. const info = analyze(item);
  74. if (info) {
  75. // using saveStyle directly since json was parsed in background page context
  76. return BG.saveStyle(Object.assign(item, SAVE_OPTIONS))
  77. .then(style => account({style, info, resolve}));
  78. }
  79. }
  80. renderQueue.forEach(style => handleUpdate(style, {reason: 'import'}));
  81. renderQueue.length = 0;
  82. done(resolve);
  83. }
  84. function analyze(item) {
  85. if (!item || !item.name || !item.name.trim() || typeof item !== 'object'
  86. || (item.sections && typeof item.sections.slice !== 'function')) {
  87. stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`);
  88. return;
  89. }
  90. item.name = item.name.trim();
  91. const byId = BG.cachedStyles.byId.get(item.id);
  92. const byName = oldStylesByName.get(item.name);
  93. oldStylesByName.delete(item.name);
  94. let oldStyle;
  95. if (byId) {
  96. if (sameStyle(byId, item)) {
  97. oldStyle = byId;
  98. } else {
  99. item.id = null;
  100. }
  101. }
  102. if (!oldStyle && byName) {
  103. item.id = byName.id;
  104. oldStyle = byName;
  105. }
  106. const oldStyleKeys = oldStyle && Object.keys(oldStyle);
  107. const metaEqual = oldStyleKeys &&
  108. oldStyleKeys.length === Object.keys(item).length &&
  109. oldStyleKeys.every(k => k === 'sections' || oldStyle[k] === item[k]);
  110. const codeEqual = oldStyle && BG.styleSectionsEqual(oldStyle, item);
  111. if (metaEqual && codeEqual) {
  112. stats.unchanged.names.push(oldStyle.name);
  113. stats.unchanged.ids.push(oldStyle.id);
  114. return;
  115. }
  116. return {oldStyle, metaEqual, codeEqual};
  117. }
  118. function sameStyle(oldStyle, newStyle) {
  119. return oldStyle.name.trim() === newStyle.name.trim() ||
  120. ['updateUrl', 'originalMd5', 'originalDigest']
  121. .some(field => oldStyle[field] && oldStyle[field] === newStyle[field]);
  122. }
  123. function account({style, info, resolve}) {
  124. renderQueue.push(style);
  125. if (performance.now() - lastRenderTime > RENDER_NAP_TIME_MAX
  126. || renderQueue.length > RENDER_QUEUE_MAX) {
  127. renderQueue.forEach(style => handleUpdate(style, {reason: 'import'}));
  128. setTimeout(scrollElementIntoView, 0, $('#style-' + renderQueue.pop().id));
  129. renderQueue.length = 0;
  130. lastRenderTime = performance.now();
  131. }
  132. setTimeout(proceed, 0, resolve);
  133. const {oldStyle, metaEqual, codeEqual} = info;
  134. if (!oldStyle) {
  135. stats.added.names.push(style.name);
  136. stats.added.ids.push(style.id);
  137. return;
  138. }
  139. if (!metaEqual && !codeEqual) {
  140. stats.metaAndCode.names.push(reportNameChange(oldStyle, style));
  141. stats.metaAndCode.ids.push(style.id);
  142. return;
  143. }
  144. if (!codeEqual) {
  145. stats.codeOnly.names.push(style.name);
  146. stats.codeOnly.ids.push(style.id);
  147. return;
  148. }
  149. stats.metaOnly.names.push(reportNameChange(oldStyle, style));
  150. stats.metaOnly.ids.push(style.id);
  151. }
  152. function done(resolve) {
  153. const numChanged = stats.metaAndCode.names.length +
  154. stats.metaOnly.names.length +
  155. stats.codeOnly.names.length +
  156. stats.added.names.length;
  157. Promise.resolve(numChanged && refreshAllTabs()).then(() => {
  158. const report = Object.keys(stats)
  159. .filter(kind => stats[kind].names.length)
  160. .map(kind => {
  161. const {ids, names, legend} = stats[kind];
  162. const listItemsWithId = (name, i) =>
  163. $element({dataset: {id: ids[i]}, textContent: name});
  164. const listItems = name =>
  165. $element({textContent: name});
  166. const block =
  167. $element({tag: 'details', dataset: {id: kind}, appendChild: [
  168. $element({tag: 'summary', appendChild:
  169. $element({tag: 'b', textContent: names.length + ' ' + t(legend)})
  170. }),
  171. $element({tag: 'small', appendChild:
  172. names.map(ids ? listItemsWithId : listItems)
  173. }),
  174. ]});
  175. return block;
  176. });
  177. scrollTo(0, 0);
  178. messageBox({
  179. title: t('importReportTitle'),
  180. contents: report.length ? report : t('importReportUnchanged'),
  181. buttons: [t('confirmOK'), numChanged && t('undo')],
  182. onshow: bindClick,
  183. }).then(({button}) => {
  184. if (button === 1) {
  185. undo();
  186. }
  187. });
  188. resolve(numChanged);
  189. });
  190. }
  191. function undo() {
  192. const oldStylesById = new Map(oldStyles.map(style => [style.id, style]));
  193. const newIds = [
  194. ...stats.metaAndCode.ids,
  195. ...stats.metaOnly.ids,
  196. ...stats.codeOnly.ids,
  197. ...stats.added.ids,
  198. ];
  199. let resolve;
  200. index = 0;
  201. return new Promise(resolve_ => {
  202. resolve = resolve_;
  203. undoNextId();
  204. }).then(refreshAllTabs)
  205. .then(() => messageBox({
  206. title: t('importReportUndoneTitle'),
  207. contents: newIds.length + ' ' + t('importReportUndone'),
  208. buttons: [t('confirmOK')],
  209. }));
  210. function undoNextId() {
  211. if (index === newIds.length) {
  212. resolve();
  213. return;
  214. }
  215. const id = newIds[index++];
  216. deleteStyleSafe({id, notify: false}).then(id => {
  217. const oldStyle = oldStylesById.get(id);
  218. if (oldStyle) {
  219. saveStyleSafe(Object.assign(oldStyle, SAVE_OPTIONS))
  220. .then(undoNextId);
  221. } else {
  222. undoNextId();
  223. }
  224. });
  225. }
  226. }
  227. function bindClick() {
  228. const highlightElement = event => {
  229. const styleElement = $('#style-' + event.target.dataset.id);
  230. if (styleElement) {
  231. scrollElementIntoView(styleElement);
  232. animateElement(styleElement);
  233. }
  234. };
  235. for (const block of $$('details')) {
  236. if (block.dataset.id !== 'invalid') {
  237. block.style.cursor = 'pointer';
  238. block.onclick = highlightElement;
  239. }
  240. }
  241. }
  242. function limitString(s, limit = 100) {
  243. return s.length <= limit ? s : s.substr(0, limit) + '...';
  244. }
  245. function reportNameChange(oldStyle, newStyle) {
  246. return newStyle.name !== oldStyle.name
  247. ? oldStyle.name + ' —> ' + newStyle.name
  248. : oldStyle.name;
  249. }
  250. function refreshAllTabs() {
  251. return Promise.all([
  252. getActiveTab(),
  253. getOwnTab(),
  254. ]).then(([activeTab, ownTab]) => new Promise(resolve => {
  255. // list all tabs including chrome-extension:// which can be ours
  256. queryTabs().then(tabs => {
  257. const lastTab = tabs[tabs.length - 1];
  258. for (const tab of tabs) {
  259. // skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
  260. if (FIREFOX && !tab.width) {
  261. if (tab === lastTab) {
  262. resolve();
  263. }
  264. continue;
  265. }
  266. getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => {
  267. const message = {method: 'styleReplaceAll', styles};
  268. if (tab.id === ownTab.id) {
  269. applyOnMessage(message);
  270. } else {
  271. message.tabId = tab.id;
  272. invokeOrPostpone(tab.id === activeTab.id, sendMessage, message, ignoreChromeError);
  273. }
  274. setTimeout(BG.updateIcon, 0, tab, styles);
  275. if (tab === lastTab) {
  276. resolve();
  277. }
  278. });
  279. }
  280. });
  281. }));
  282. }
  283. }
  284. $('#file-all-styles').onclick = () => {
  285. getStylesSafe().then(styles => {
  286. const text = JSON.stringify(styles, null, '\t');
  287. const blob = new Blob([text], {type: 'application/json'});
  288. const objectURL = URL.createObjectURL(blob);
  289. let link = $element({
  290. tag:'a',
  291. href: objectURL,
  292. type: 'application/json',
  293. download: generateFileName(),
  294. });
  295. // TODO: remove the fallback when FF multi-process bug is fixed
  296. if (!FIREFOX) {
  297. link.dispatchEvent(new MouseEvent('click'));
  298. setTimeout(() => URL.revokeObjectURL(objectURL));
  299. } else {
  300. const iframe = document.body.appendChild($element({
  301. tag: 'iframe',
  302. style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'),
  303. }));
  304. doTimeout()
  305. .then(() => {
  306. link = iframe.contentDocument.importNode(link, true);
  307. iframe.contentDocument.body.appendChild(link);
  308. })
  309. .then(() => doTimeout())
  310. .then(() => link.dispatchEvent(new MouseEvent('click')))
  311. .then(() => doTimeout(1000))
  312. .then(() => {
  313. URL.revokeObjectURL(objectURL);
  314. iframe.remove();
  315. });
  316. }
  317. });
  318. function doTimeout(ms) {
  319. return new Promise(resolve => setTimeout(resolve, ms));
  320. }
  321. function generateFileName() {
  322. const today = new Date();
  323. const dd = ('0' + today.getDate()).substr(-2);
  324. const mm = ('0' + (today.getMonth() + 1)).substr(-2);
  325. const yyyy = today.getFullYear();
  326. return `stylus-${yyyy}-${mm}-${dd}${STYLUS_BACKUP_FILE_EXT}`;
  327. }
  328. };
  329. $('#unfile-all-styles').onclick = () => {
  330. importFromFile({fileTypeFilter: STYLUS_BACKUP_FILE_EXT});
  331. };
  332. Object.assign(document.body, {
  333. ondragover(event) {
  334. const hasFiles = event.dataTransfer.types.includes('Files');
  335. event.dataTransfer.dropEffect = hasFiles || event.target.type === 'search' ? 'copy' : 'none';
  336. this.classList.toggle('dropzone', hasFiles);
  337. if (hasFiles) {
  338. event.preventDefault();
  339. clearTimeout(this.fadeoutTimer);
  340. this.classList.remove('fadeout');
  341. }
  342. },
  343. ondragend() {
  344. animateElement(this, {className: 'fadeout', removeExtraClasses: ['dropzone']}).then(() => {
  345. this.style.animationDuration = '';
  346. });
  347. },
  348. ondragleave(event) {
  349. try {
  350. // in Firefox event.target could be XUL browser and hence there is no permission to access it
  351. if (event.target === this) {
  352. this.ondragend();
  353. }
  354. } catch (e) {
  355. this.ondragend();
  356. }
  357. },
  358. ondrop(event) {
  359. this.ondragend();
  360. if (event.dataTransfer.files.length) {
  361. event.preventDefault();
  362. if ($('#onlyUpdates input').checked) {
  363. $('#onlyUpdates input').click();
  364. }
  365. importFromFile({file: event.dataTransfer.files[0]});
  366. }
  367. },
  368. });