This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
112 lines (98 loc) · 3.25 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint asi: true undef: true es5: true node: true devel: true
forin: true latedef: false browser: true */
/*global define: true port: true */
!define(function(require, exports) {
"use strict";
var data = require("self").data
var PageMod = require("addon-kit/page-mod").PageMod
var protocol = require("raw.github.com/Gozala/jetpack-protocol/v0.3.0/index")
var fs = require("raw.github.com/Gozala/jetpack-io/v0.4.2/fs")
var permissions = require('./permissions')
const PROTOCOL = 'edit'
const editorURI = data.url('index.html')
const rootURI = editorURI.substr(0, editorURI.lastIndexOf('/') + 1)
console.log(editorURI)
function errorToJSON(error) {
return error ? { message: error.message, stack: error.stack } : error
}
// Allow popups for editor.
permissions.add({
host: 'edit:',
type: 'popup',
capability: true
})
var mod = PageMod({
include: PROTOCOL + ':*',
contentScript: 'unsafeWindow.port = self.port',
contentScriptWhen: 'start',
onAttach: function onAttach(worker) {
worker.port.on('<=', function onMessage(message) {
fs[message.method].apply(fs, message.params.concat([function(error) {
worker.port.emit('=>', {
'@': message['@'],
params: [errorToJSON(error)].concat(Array.slice(arguments, 1))
})
}]))
})
}
})
function isAbsolute(uri) {
return ~uri.indexOf('edit:') || ~uri.indexOf('://')
}
function resolve(id, base) {
var path, paths, last
if (isAbsolute(id)) return id
paths = id.split('/')
base = base ? base.split('/') : [ '.' ]
if (base.length > 1) base.pop()
while ((path = paths.shift())) {
if (path === '..') {
if (base.length && base[base.length - 1] !== '..') {
if (base.pop() === '.') base.push(path)
} else base.push(path)
} else if (path !== '.') {
base.push(path)
}
}
if (base[base.length - 1].substr(-1) === '.') base.push('')
return base.join('/')
}
function normalize(uri) {
return uri.replace(/\/\.\//, '/')
}
// Registers protocol handler for `edit:*` protocol.
var editProtocolHandler = protocol.protocol(PROTOCOL, {
onResolve: function(uri, base) {
if (base && !~base.indexOf('edit:///'))
base = 'edit:///index.html'
var href = normalize(resolve(uri, base)).
replace('edit::', '') // strip out host from the URLs.
return {
scheme: 'edit',
host: 'edit:',
href: href,
path: ~href.indexOf('edit:///') ? href.replace('edit:///', '') : '/'
}
},
// When browser is navigated to `edit:*` URI this function is called with an
// absolute URI and returned content or content under returned URI will be
// displayed to a user.
onRequest: function(request, response) {
try {
// All the editor content is located under 'edit:///' so to get a path
// we just strip that out.
var path = request.uri.replace('edit:///', '')
// If requested path was diff from 'edit:///...', then we load editor.
path = ~path.indexOf('edit:') ? 'index.html' : path
response.uri = data.url(path)
} catch(error) {
console.exception(error)
}
}
}).register()
// Make suer that protocol handler is removed, once add-on is uninstalled.
require("unload").when(function() {
editProtocolHandler.unregister()
})
});