From a618964b2db8149f1f395076e24e1e15b717a0bf Mon Sep 17 00:00:00 2001 From: w8tcha Date: Sat, 24 Dec 2016 17:16:37 +0100 Subject: [PATCH] prevent browser error when localstorage is full fixes #67 --- autosave/lang/ca.js | 1 + autosave/lang/cs.js | 1 + autosave/lang/de.js | 1 + autosave/lang/en.js | 3 ++- autosave/lang/es.js | 1 + autosave/lang/fr.js | 1 + autosave/lang/ja.js | 1 + autosave/lang/nl.js | 1 + autosave/lang/pl.js | 1 + autosave/lang/pt-br.js | 1 + autosave/lang/sk.js | 1 + autosave/lang/sv.js | 4 ++- autosave/lang/zh-cn.js | 1 + autosave/lang/zh.js | 1 + autosave/plugin.js | 56 +++++++++++++++++++++++++++++++++++------- 15 files changed, 64 insertions(+), 11 deletions(-) diff --git a/autosave/lang/ca.js b/autosave/lang/ca.js index ad8d84d..bbf41bf 100644 --- a/autosave/lang/ca.js +++ b/autosave/lang/ca.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'ca', { loadSavedContent: 'S\'ha trobat una versió auto-desada d\'aquest contingut de "{0}". Voleu comparar les versions del contingut i escollir quina carregar?', title: 'Compareu el contingut auto-desat amb el contingut carregat del lloc web', loadedContent: 'Contingut carregat', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Contingut auto-desat de: \'', ok: 'Sí, carrega el contingut auto-desat', no: 'No', diff --git a/autosave/lang/cs.js b/autosave/lang/cs.js index d3406a9..d2c1ba8 100644 --- a/autosave/lang/cs.js +++ b/autosave/lang/cs.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'cs', { loadSavedContent: 'byla nalezena automaticky uložená verze obsahu ze dne "{0}". Chcete porovnat obsah a zvolit si s kterým obsahem chete pracovat?', title: 'Porovnejre automaticky uložený obsah s obsahem nahraným z webové stránky', loadedContent: 'Nahraný obsah', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Automaticky uložený obsah z: \'', ok: 'Ano, zvolit automaticky uložený obsah', no: 'Ne', diff --git a/autosave/lang/de.js b/autosave/lang/de.js index 6e93473..6f525e7 100644 --- a/autosave/lang/de.js +++ b/autosave/lang/de.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'de', { loadSavedContent: 'Es wurde ein autom. Gespeicherter Inhalt gefunden (vom "{0}"). Wollen Sie den Inhalt vergleichen, um dann entscheiden zu können ob der Inhalt geladen werden soll?', title: 'Temp. Gespeichert Inhalt vergleichen', loadedContent: 'Geladener Inhalt', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Autom. Gespeicherten Inhalt (vom: \'', ok: 'Ja, Lade Autom. Gespeicherten Inhalt', no: 'Nein', diff --git a/autosave/lang/en.js b/autosave/lang/en.js index 866f4af..ddae7f7 100644 --- a/autosave/lang/en.js +++ b/autosave/lang/en.js @@ -7,7 +7,8 @@ CKEDITOR.plugins.setLang('autosave', 'en', { autoSaveMessage: 'Auto Saved', loadSavedContent: 'An auto-saved version of this content from "{0}" has been found. Would you like to compare content versions and choose which one to load? Clicking Cancel will remove previously auto-saved content.', title: 'Compare auto-saved content with that loaded from the website', - loadedContent: 'Loaded content', + loadedContent: 'Loaded content', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Auto-saved content from: \'', ok: 'Yes, load auto-saved content', no: 'No', diff --git a/autosave/lang/es.js b/autosave/lang/es.js index c188929..46a4016 100644 --- a/autosave/lang/es.js +++ b/autosave/lang/es.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'es', { loadSavedContent: 'Se ha encontrado una versión auto-guardada de este contenido de "{0}". Queréis comparar el contenido de las versiones y elegir cual cargar?', title: 'Compara el contenido auto-guardado con el cargado del sitio web', loadedContent: 'Contenido cargado', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Contenido auto-guardado de: \'', ok: 'Si, carga el contenido auto-guardado', no: 'No', diff --git a/autosave/lang/fr.js b/autosave/lang/fr.js index 1b456b7..4faa676 100644 --- a/autosave/lang/fr.js +++ b/autosave/lang/fr.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'fr', { loadSavedContent: 'Une précédente version a été sauvegardée le "{0}". Voulez-vous l\'afficher et la comparer avec le document en cours ?', title: 'Comparaison du document en cours et du document sauvegardé', loadedContent: 'Document en cours', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Document sauvegardé le : \'', ok: 'Oui, charger le document sauvegardé', no: 'Non, continuer avec le document en cours', diff --git a/autosave/lang/ja.js b/autosave/lang/ja.js index 61fcf1b..a931a12 100644 --- a/autosave/lang/ja.js +++ b/autosave/lang/ja.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'ja', { loadSavedContent: '投稿途中だった内容があります({0})。現在保存されている投稿と比較しますか?', title: 'Compare auto-saved content with that loaded from the website', loadedContent: '現在保存されている投稿', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: '投稿途中だった内容: \'', ok: '投稿途中だった内容(緑)を使う', no: 'いいえ', diff --git a/autosave/lang/nl.js b/autosave/lang/nl.js index f65de80..a01f452 100644 --- a/autosave/lang/nl.js +++ b/autosave/lang/nl.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'nl', { loadSavedContent: 'Een automatisch opgeslagen versie van deze inhoud van "{0}" is gevonden. Wilt u de inhoud van deze versies vergelijken en kiezen welke te laden?', title: 'Vergelijk automatisch opgeslagen inhoud met die geladen van de website', loadedContent: 'Geladen inhoud', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Automatisch opgeslagen inhoud van: \'', ok: 'Ja, laadt automatisch opgeslagen inhoud', no: 'Nee', diff --git a/autosave/lang/pl.js b/autosave/lang/pl.js index 15b5890..b0d1f43 100644 --- a/autosave/lang/pl.js +++ b/autosave/lang/pl.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'pl', { loadSavedContent: 'Istnieje automatycznie zapisana wersja zawartości (z "{0}"). Otworzyć okno porównawcze, w którym można zadecydować o wczytaniu automatycznie zapisanej zawartości?', title: 'Porównanie tymczasowo zapisanej zawartości z zawartością wczytaną w witrynie', loadedContent: 'Zawartość wczytania', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Zawartość zapisana automatycznie (z: \'', ok: 'Tak, wczytaj automatycznie zapisaną treść', no: 'Nie', diff --git a/autosave/lang/pt-br.js b/autosave/lang/pt-br.js index 7bf9f21..9c171ff 100644 --- a/autosave/lang/pt-br.js +++ b/autosave/lang/pt-br.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'pt-br', { loadSavedContent: 'An auto-saved version of this content from "{0}" has been found. Would you like to compare content versions and choose which one to load?', title: 'Compare auto-saved content with that loaded from the website', loadedContent: 'Loaded content', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Auto-saved content from: \'', ok: 'Yes, load auto-saved content', no: 'No', diff --git a/autosave/lang/sk.js b/autosave/lang/sk.js index 22373d3..75555c6 100644 --- a/autosave/lang/sk.js +++ b/autosave/lang/sk.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'sk', { loadSavedContent: 'Bola nájdená automaticky uložená verzia z "{0}". Chcete porovnať obsah a vybrať verziu, ktorú chcete použiť?', title: 'Porovnanie automaticky uloženého obsahu s obsahom nahraným z webu', loadedContent: 'Nahraný obsah z webu', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Automaticky uložený obsah z: \'', ok: 'Použiť automaticky uložený obsah', no: 'Použiť obsah z webu', diff --git a/autosave/lang/sv.js b/autosave/lang/sv.js index b150018..f0dd509 100644 --- a/autosave/lang/sv.js +++ b/autosave/lang/sv.js @@ -4,10 +4,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license */ CKEDITOR.plugins.setLang('autosave', 'sv', { dateFormat: 'LLL', - autoSaveMessage: 'Autosparat', + autoSaveMessage: 'Auto Saved', + loadSavedContent: 'There is a auto-saved version of this content (From "{0}") found. Do you want to open the Compare Dialog, where you can decide to load the auto saved Content?', loadSavedContent: 'En auto-sparad version av det här innehållet (från "{0}") hittades. Vill du öppna en dialogruta för att jämföra, där du även kan välja att läsa in det auto sparade innehållet?', title: 'Jämför Temporärt sparat innehåll med det inladdade innehållet från sidan', loadedContent: 'Inladdat innehåll', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Autosparat innehåll (från: \'', ok: 'Läs in Autosparat innehåll', no: 'Nej tack', diff --git a/autosave/lang/zh-cn.js b/autosave/lang/zh-cn.js index dabbec8..54363a4 100644 --- a/autosave/lang/zh-cn.js +++ b/autosave/lang/zh-cn.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'zh-cn', { loadSavedContent: '已经存在这个内容之前保存的版本 "{0}". 你希望比较两个不同的版本并从中选择你想要加载的那个么?', title: '比较自动保存的版本', loadedContent: '已经加载的内容', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: '自动保存的内容: \'', ok: '我确认,加载自动保存的内容', no: '取消', diff --git a/autosave/lang/zh.js b/autosave/lang/zh.js index aa8e2bb..2f48c84 100644 --- a/autosave/lang/zh.js +++ b/autosave/lang/zh.js @@ -8,6 +8,7 @@ CKEDITOR.plugins.setLang('autosave', 'zh', { loadSavedContent: 'An auto-saved version of this content from "{0}" has been found. Would you like to compare content versions and choose which one to load?', title: 'Compare auto-saved content with that loaded from the website', loadedContent: 'Loaded content', + localStorageFull: 'Browser localStorage is full, clear your storage or Increase database size', autoSavedContent: 'Auto-saved content from: \'', ok: 'Yes, load auto-saved content', no: 'No', diff --git a/autosave/plugin.js b/autosave/plugin.js index 3961e9b..62416e8 100644 --- a/autosave/plugin.js +++ b/autosave/plugin.js @@ -243,27 +243,42 @@ function SaveData(autoSaveKey, editorInstance, config) { var compressedJSON = LZString.compressToUTF16(JSON.stringify({ data: editorInstance.getData(), saveTime: new Date() })); - localStorage.setItem(autoSaveKey, compressedJSON); - var messageType = config.messageType != null ? config.messageType : "notification"; + var quotaExceeded = false; - if (editorInstance.plugins.textselection && messageType == "statusbar") { - messageType = "notification"; + try { + localStorage.setItem(autoSaveKey, compressedJSON); + } catch (e) { + quotaExceeded = isQuotaExceeded(e); + if (quotaExceeded) { + console.log(editorInstance.lang.autosave.localStorageFull); + } } - if (messageType == "statusbar") { + if (quotaExceeded) { + var notificationError = new CKEDITOR.plugins.notification(editorInstance, { message: editorInstance.lang.autosave.localStorageFull, type: 'warning' }); + notificationError.show(); + } else { + var messageType = config.messageType != null ? config.messageType : "notification"; + + if (editorInstance.plugins.textselection && messageType == "statusbar") { + messageType = "notification"; + } + + if (messageType == "statusbar") { var autoSaveMessage = document.getElementById(autoSaveMessageId(editorInstance)); if (autoSaveMessage) { autoSaveMessage.className = "show"; - setTimeout(function() { + setTimeout(function () { autoSaveMessage.className = "hidden"; }, 2000); } - } else if (messageType == "notification") { - var notification = new CKEDITOR.plugins.notification(editorInstance, { message: editorInstance.lang.autosave.autoSaveMessage, type: 'success' }); - notification.show(); + } else if (messageType == "notification") { + var notification = new CKEDITOR.plugins.notification(editorInstance, { message: editorInstance.lang.autosave.autoSaveMessage, type: 'success' }); + notification.show(); + } } } @@ -293,4 +308,27 @@ viewType: dialog.getContentElement('general', 'diffType').getValue() == "inline" ? 1 : 0 }).outerHTML + ''); } + + function isQuotaExceeded(e) { + var quotaExceeded = false; + if (e) { + if (e.code) { + switch (e.code) { + case 22: + quotaExceeded = true; + break; + case 1014: + // Firefox + if (e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { + quotaExceeded = true; + } + break; + } + } else if (e.number === -2147024882) { + // Internet Explorer 8 + quotaExceeded = true; + } + } + return quotaExceeded; + } })();