-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
91 lines (86 loc) · 2.49 KB
/
index.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
'use strict';
var {Cc, Ci, Cu} = require('chrome');
var {Downloads} = Cu.import('resource://gre/modules/Downloads.jsm');
var {OS} = Cu.import('resource://gre/modules/osfile.jsm');
var notifications = require('sdk/notifications');
Cu.importGlobalProperties(['Blob']);
Cu.importGlobalProperties(['URL']);
var service;
try {
service = Cc['@userstyles.org/style;1'].getService(Ci.stylishStyle);
}
catch (e) {
notifications.notify({
title: 'Stylish export to JSON',
text: 'Stylish add-on is not installed or is not enabled. To extract styles make sure Stylish add-on is enabled, then reload this extension.'
});
}
var styles = service.list(service.REGISTER_STYLE_ON_CHANGE, {}).map(s => {
// changing update URL to chrome version
if (s.updateUrl) {
const id = /\d+/.exec(s.updateUrl);
if (id && id.length) {
let args = '';
if (s.updateUrl.indexOf('?') !== -1) {
args = s.updateUrl.split('?').pop();
}
s.updateUrl = `https://userstyles.org/styles/chrome/${id[0]}.json` + (args ? '?' + args : '');
}
}
return {
enabled: s.enabled,
updateUrl: s.updateUrl,
md5Url: s.md5Url,
url: s.url,
originalMd5: s.originalMd5,
sections: [],
name: s.name,
id: s.id,
code: s.code
};
});
function prepare (style) {
let sections = [];
return new Promise((resolve) => {
const onSection = (e, section) => sections.push(section);
const onError = () => {};
const onDone = () => {
delete style.code;
sections = sections.filter(s => s.code.trim()).map(s => {
delete s.start;
s = Object.assign({
code: '',
urls: [],
urlPrefixes: [],
domains: [],
regexps: []
}, s);
return s;
});
resolve(Object.assign(style, {sections}));
};
try {
require('./parse').fromMozillaFormat(style.code, onSection, onError, onDone);
}
catch (e) {
console.error(style.name, e.message);
resolve();
}
});
}
Promise.all(styles.map(prepare)).then(styles => {
styles = styles.filter(s => s);
const path = OS.Path.join(OS.Constants.Path.desktopDir, 'stylish.json');
//styles.map(s => s.sections.map(s => console.error(s.code)));
Downloads.fetch(
URL.createObjectURL(new Blob([JSON.stringify(styles, null, '\t')], {
type: 'text/plain;charset=utf-8;'
})),
path
).then(() => {
notifications.notify({
title: 'Stylish export to JSON',
text: `Exported ${styles.length} styles to "${path}"`
});
});
});