Skip to content

Commit 58035a0

Browse files
committed
Fixes richgilbank#61 : save code in localstorage
1 parent 8c997f2 commit 58035a0

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
dist
44
Scratch\ JS.zip
55
panel/styles/repl.css
6+
.idea

background.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
2-
if (request.name === 'platformInfo') {
3-
chrome.runtime.getPlatformInfo(function(info) {
4-
sendResponse(info);
5-
});
6-
return true; // indicates sendResponse will be called asynchronously
7-
} else if(request.name === 'getSettings') {
8-
sendResponse(JSON.parse(localStorage.getItem('settings')));
9-
} else if(request.name === 'setSettings') {
10-
localStorage.setItem('settings', JSON.stringify(request.value));
11-
sendResponse(localStorage.getItem('settings'));
2+
switch (request.name) {
3+
case 'platformInfo':
4+
chrome.runtime.getPlatformInfo(function(info) {
5+
sendResponse(info);
6+
});
7+
return true; // indicates sendResponse will be called asynchronously
8+
break;
9+
case 'getSettings':
10+
sendResponse(JSON.parse(localStorage.getItem('settings')));
11+
break;
12+
case 'setSettings':
13+
localStorage.setItem('settings', JSON.stringify(request.value));
14+
sendResponse(localStorage.getItem('settings'));
15+
break;
16+
case 'getCode':
17+
chrome.storage.sync.get('code', function (data) {
18+
sendResponse(data);
19+
});
20+
return true;
21+
break;
22+
case 'setCode':
23+
chrome.storage.sync.set({'code': request.value}, function () {
24+
sendResponse('success');
25+
});
26+
return true;
27+
break;
1228
}
1329
});

panel/scripts/repl.js

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ Repl.prototype.onDomReady = function() {
5454
$('#combinationKey')[0].textContent = 'Ctrl';
5555
}
5656
});
57+
58+
var editor = this.editor;
59+
chrome.runtime.sendMessage({name: 'getCode'}, function(data) {
60+
if (data.code) {
61+
editor.setValue(data.code);
62+
}
63+
});
5764
}
5865

5966
Repl.prototype.loadContexts = function() {
@@ -163,6 +170,10 @@ Repl.prototype.resizeOutput = function(e) {
163170
this.DOM.output.style.width = 100 - percentWidth + "%";
164171
};
165172

173+
Repl.prototype.saveCode = function() {
174+
chrome.runtime.sendMessage({name: 'setCode', value: this.editor.getValue()});
175+
};
176+
166177
Repl.prototype.addEventListeners = function() {
167178
var _this = this;
168179

@@ -179,6 +190,7 @@ Repl.prototype.addEventListeners = function() {
179190
});
180191

181192
document.addEventListener('keydown', debounce(_this.updateOutput, 200, _this));
193+
document.addEventListener('keydown', debounce(_this.saveCode, 1000, _this));
182194
document.addEventListener('keydown', function(e) {
183195
if(e[combinationKey] && e.which == 13) {
184196
_this.deliverContent(_this.editor.getValue());

0 commit comments

Comments
 (0)