getStorageScript function

String getStorageScript()

Implementation

String getStorageScript() {
  const getStorage = 'GET_STORAGE';
  const setStorage = 'SET_STORAGE';
  const storageReady = 'STORAGE_READY';
  const storageError = 'STORAGE_ERROR';
  const storageResponse = 'STORAGE_RESPONSE';
  final channel = kWebViewMessageChannelName;
  return '''
  window.handleStorageOperation = function(message) {
    try {
      switch(message.type) {
        case '$getStorage':
          var value = localStorage.getItem(message.key);
          window.$channel.postMessage(JSON.stringify({
            type: '$storageResponse',
            key: message.key,
            value: value
          }));
          break;
        case '$setStorage':
          localStorage.setItem(message.key, message.value);
          window.$channel.postMessage(JSON.stringify({
            type: '$storageResponse',
            key: message.key,
            value: message.value
          }));
          break;
      }
    } catch (error) {
      window.$channel.postMessage(JSON.stringify({
        type: '$storageError',
        error: error.message
      }));
    }
  };
  window.$channel.postMessage(JSON.stringify({
    type: '$storageReady'
  }));
  true;
''';
}