-
Notifications
You must be signed in to change notification settings - Fork 123
/
stylish0.5_edit.uc.js
409 lines (374 loc) · 16.3 KB
/
stylish0.5_edit.uc.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// ==UserScript==
// @name stylish0.5_edit.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description External Edittor for Stylish-editor, For stylish 0.5~
// @include main
// @compatibility Firefox 2.0 3.0
// @author Original Author: pile0nades
// @modifier Alice0775
// @version 2014/02/25 textarea, orion Firefox24, codemirror Firefox27+,(orion,codemirrorの時はcolorpicker無し)
// @version 2009/04/30 version 1.0対応(tnks 音吉) でも0.59で不具合がないなら1.0にしない方がいいよ
// @version 2007/05/25
// @Note
// ==/UserScript==
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above.
*
* The Original Code is the important.uc.js .
* The Initial Developer of the above Original Code is
* pile0nades.
*
* The Original Code is the External Editor extension.
* The Initial Developer of the above Original Code is
* Philip Nilsson.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kimitake
* Supported Japanese charaset and added ja-JP locale
*
* The Original Code is the MozEx extension.
* Copyright (C) 2003 Tomas Styblo <tripie@cpan.org>
*
*
* Contributor(s):
* External Edittor for Stylish-editor, For stylish 0.5~
* Alice0775
* http://space.geocities.yahoo.co.jp/gl/alice0775
*
* ***** END LICENSE BLOCK ***** */
WindowHook.register("chrome://stylish/content/edit.xul",
function winhook(aWindow) {
// get the checkbox
var checkbox = aWindow.document.getElementById("wrap-lines");
if (aWindow.document.getElementById("internal-code")) { //ver 1.0 のとき color picker と importantを入れる
//Color picker///////////////////////////////////////////////////////////////////////////////////
if('rainbowpickerToolbar' in window) {
var menuitem = aWindow.document.createElement("menuitem");
button.setAttribute("label", "Color Picker");
checkbox.parentNode.insertBefore(button, checkbox);
button.addEventListener("click", function() {
aWindow.openColorPicker = function() {
var open = rainbowpickerToolbar();
}
aWindow.openColorPicker();
}, false);
} else if (aWindow.prefs.getIntPref("editor") == 1) {
var picker = aWindow.document.createElement("colorpicker");
var getColor = function(){
if (arguments.callee.caller.name != 'onchange') return; //why is this necessary?
var box = aWindow.document.getElementById("internal-code").mInputField;
var text = box.value.toString()
var s = box.selectionStart;
var e = box.selectionEnd;
box.value = text.substring(0,s) +
this.color.toString() +
text.substring(e,text.length);
box.setSelectionRange(s + 7, s + 7); // 7 = "#AABBCC".length
box.focus();
}
picker.getColor = getColor;
picker.setAttribute("label", "Color Picker");
picker.setAttribute("type", 'button');
picker.setAttribute("onchange", "this.getColor()");
checkbox.parentNode.insertBefore(picker, checkbox);
}
//!important////////////////////////////////////////////////////////////////////////////////////////
// create a button and place it
var button = aWindow.document.createElement("button");
button.setAttribute("label", "!important");
checkbox.parentNode.insertBefore(button, checkbox);
// add click event to button
button.addEventListener("click", function() {
if (aWindow.sourceEditorType == "orion" || aWindow.sourceEditorType == "sourceeditor") {
var codeElement = aWindow.document.getElementById("sourceeditor");
var code = aWindow.sourceEditor.getText();
} else {
var codeElement = aWindow.document.getElementById("code");
if (!codeElement)
codeElement = aWindow.document.getElementById("internal-code");
var box = codeElement.mInputField;
var scroll = [box.scrollTop, box.scrollLeft];
var code = codeElement.value;
}
code = code.replace(/\s*?!\s*?important/gi, "") // remove existing !important's to simplify things
//change ;base64 to __base64__ so we don't match it on the ; when we split declarations
code = code.replace(/;base64/g, "__base64__");
var declarationBlocks = code.match(/\{[^\{\}]*[\}]/g);
var declarations = [];
declarationBlocks.forEach(function (declarationBlock) {
declarations = declarations.concat(declarationBlock.split(/;/));
});
//make sure everything is really a declaration, and make sure it's not already !important
declarations = declarations.filter(function (declaration) {
return /[A-Za-z0-9-]+\s*:\s*[^};]+/.test(declaration) && !/!important/.test(declaration);
});
//strip out any extra stuff like brackets and whitespace
declarations = declarations.map(function (declaration) {
return declaration.match(/[A-Za-z0-9-]+\s*:\s*[^};]+/)[0].replace(/\s+$/, "");
});
//replace them with "hashes" to avoid a problem with multiple identical name/value pairs
var replacements = [];
declarations.forEach(function (declaration) {
var replacement = {hash: Math.random(), value: declaration};
replacements.push(replacement);
code = code.replace(replacement.value, replacement.hash);
});
replacements.forEach(function (replacement) {
code = code.replace(replacement.hash, replacement.value + " !important");
});
//put ;base64 back
code = code.replace(/__base64__/g, ";base64");
if (aWindow.sourceEditorType == "orion" || aWindow.sourceEditorType == "sourceeditor") {
aWindow.sourceEditor.setText(code);
} else {
codeElement.value = code;
box.scrollTop = scroll[0];
box.scrollLeft = scroll[1];
}
}, false);
}
//External Editor///////////////////////////////////////////////////////////////////////////////////
//if(typeof ItsAllText != 'undefined') return;
// add External Editor button
button = aWindow.document.createElement("button");
button.setAttribute("label", "Editor");
button.setAttribute("accesskey","T");
checkbox.parentNode.insertBefore(button, checkbox);
// add click event to button
button.addEventListener("click", function() {
if (aWindow.sourceEditorType == "orion" || aWindow.sourceEditorType == "sourceeditor") {
var textarea = aWindow.document.getElementById("sourceeditor");
editinit();
edittarget(textarea);
} else {
var textarea = aWindow.document.getElementById("code");
if (!textarea)
textarea = aWindow.document.getElementById("internal-code");
try{
editinit();
edittarget(textarea);
}catch(e){}
}
}, false);
////Extarnal Edittor functions///////////////////////////////////////////////////////////////////////
//Extarnal Edittor functions
//
var _editor,_tmpdir = null,_dir_separator,_os;
var _ext,_encode,_target=[];
function editinit(){
if(window.navigator.platform.toLowerCase().indexOf("win") != -1){
//_editor = "C:\\WINDOWS\\notepad.exe"; /* windows */
_editor = "C:\\progra~1\\hidemaru\\hidemaru.exe"; /* windows */
_dir_separator = '\\'; /* windows */
_os = 'win'; /* windows */
}else{
_editor = "/bin/vi"; /* unix */
_dir_separator = '/'; /* unix */
_os = 'unix'; /* unix */
}
_ext = "css";
_encode = 'UTF-8';
_target = [];
window.addEventListener("unload", function(){ edituninit(); }, false);
aWindow.addEventListener("unload", function(){
aWindow.document.removeEventListener("focus", function(){ checkfocus_window(); }, true);
}, false);
}
function edituninit(){
if(_tmpdir == null) return;
var windowType = "navigator:browser";
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
var enumerator = windowManagerInterface.getEnumerator(windowType);
if (enumerator.hasMoreElements()) {
return;
}
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(_tmpdir);
var entries = file.directoryEntries;
while (entries.hasMoreElements()) {
var entry = entries.getNext().QueryInterface(Components.interfaces.nsIFile);
if (/^ucjs.textarea\./i.test(entry.leafName)){
try{
entry.remove(false);
}catch(e){}
}
}
try{
if( file.exists() == true ) file.remove(false);
}catch(e){}
_tmpdir = null;
}
function checkfocus_window(){
var target, filename, timestamp, encode, file, inst, sstream, utf, textBoxText
if (_target.length<=0) return;
file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
istr = Components.classes['@mozilla.org/network/file-input-stream;1'].
createInstance(Components.interfaces.nsIFileInputStream);
// FileInputStream's read is [noscript].
sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].
createInstance(Components.interfaces.nsIScriptableInputStream);
utf = Components.classes['@mozilla.org/intl/utf8converterservice;1'].
createInstance(Components.interfaces.nsIUTF8ConverterService);
for(var i=0,len=_target.length;i<len;i++){
target = _target[i];
if(!target.hasAttribute("filename")) continue;
filename = target.getAttribute("filename");
timestamp = target.getAttribute("timestamp");
file.initWithPath(filename);
if(!file.exists() || !file.isReadable()) continue;
if(file.lastModifiedTime <= timestamp) continue;
target.setAttribute("timestamp", file.lastModifiedTime);
istr.init(file, 1, 0x400, false);
sstream.init(istr);
textBoxText = sstream.read(sstream.available());
encode = target.getAttribute("encode");
if (aWindow.sourceEditorType == "orion" || aWindow.sourceEditorType == "sourceeditor") {
aWindow.sourceEditor.setText(textBoxText);
} else {
if(textBoxText.length)
target.value = utf.convertStringToUTF8(textBoxText, encode, true);
else
target.value = "";
}
sstream.close();
istr.close();
try{file.remove(false);}catch(e){}
}
}
function editfile(target,filename){
// Figure out what editor to use.
var editor = _editor;
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(editor);
if(!file.exists()){
alert("Error_invalid_Editor_file");
return false;
}
if(!file.isExecutable()){
alert("Error_Editor_not_executable");
return false;
}
target.setAttribute("filename", filename);
target.setAttribute("timestamp", file.lastModifiedTime);
// Run the editor.
var process = Components.classes["@mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
process.init(file);
var args = [filename];
process.run(false, args, args.length); // don't block
aWindow.document.addEventListener("focus", function(){ checkfocus_window(); }, true);
return true;
}
function edittarget(target){
if (aWindow.sourceEditorType == "orion" || aWindow.sourceEditorType == "sourceeditor") {
var textBoxText = aWindow.sourceEditor.getText();
} else {
var textBoxText = target.value;
}
// Get filename.
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
if(target.hasAttribute("filename")){
var filename = target.getAttribute("filename");
file.initWithPath(filename);
try{
if( file.exists() == true ) file.remove(false);
}catch(e){}
}else{
var filename = TmpFilenameTextarea();
}
file.initWithPath(filename);
file.create(file.NORMAL_FILE_TYPE, parseInt(600,8));
// Write the data to the file.
var ostr = Components.classes['@mozilla.org/network/file-output-stream;1'].
createInstance(Components.interfaces.nsIFileOutputStream);
ostr.init(file, 2, 0x200, false);
if(navigator.platform == "Win32"){
// Convert Unix newlines to standard network newlines.
textBoxText = textBoxText.replace(/\n/g, "\r\n");
}
var conv = Components.classes['@mozilla.org/intl/saveascharset;1'].
createInstance(Components.interfaces.nsISaveAsCharset);
try{
conv.Init(_encode, 0, 0);
textBoxText = conv.Convert(textBoxText);
}catch(e){
textBoxText = "";
}
ostr.write(textBoxText, textBoxText.length);
ostr.flush();
ostr.close();
// setup target info
target.setAttribute("encode", _encode);
// Edit the file.
if(editfile(target,file.path)){
_target.push(target); // Editting target array
}
}
//Compose temporary filename out of
// - tmpdir setting
// - document url
// - textarea name
// - ext suffix
function TmpFilenameTextarea(){
var TmpFilename;
_tmpdir = gettmpDir();
do{
TmpFilename = _tmpdir + _dir_separator + "ucjs.textarea." +
Math.floor( Math.random() * 100000 ) + "." + _ext;
}while(!ExistsFile(TmpFilename))
return TmpFilename;
}
//Function returns true if given filename exists
function ExistsFile(filename){
try{
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filename);
return true;
}catch(e){
return false;
}
}
/**
* Returns the directory where we put files to edit.
* @returns nsILocalFile The location where we should write editable files.
*/
function gettmpDir() {
/* Where is the directory that we use. */
var fobj = Components.classes["@mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("ProfD", Components.interfaces.nsIFile);
fobj.append('Temp_ExternalEditor');
if (!fobj.exists()) {
fobj.create(Components.interfaces.nsIFile.DIRECTORY_TYPE,
parseInt('0700',8));
}
if (!fobj.isDirectory()) {
alert('Having a problem finding or creating directory: '+fobj.path);
}
return fobj.path;
}
}
);