-
Notifications
You must be signed in to change notification settings - Fork 37
/
Inspector_de_Info.uc.js
537 lines (495 loc) · 19.6 KB
/
Inspector_de_Info.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// ==UserScript==
// @name Inspector_de_Info
// @description 要素を調査で SITEINFO を書く
// @namespace http://d.hatena.ne.jp/Griever/
// @author Griever
// @license MIT License
// @compatibility Firefox 32
// @charset UTF-8
// @include main
// @screenshot http://f.hatena.ne.jp/Griever/20120409200608
// @version 0.0.4
// @note 0.0.4 なんかいじった
// @note 0.0.3 CSS Selector も取得してみた
// @note 0.0.3 E4X の修正忘れを修正
// @note 0.0.2 Remove E4X
// ==/UserScript==
(function(CSS){
if (window.Inspector_de_Info) {
window.Inspector_de_Info.destroy();
delete window.Inspector_de_Info;
}
window.Inspector_de_Info = {
getFocusedWindow: function(){
var win = document.commandDispatcher.focusedWindow;
return (!win || win == window) ? window.content : win;
},
ready: function(subject, aToolbox) {
var doc = aToolbox.doc;
var win = doc.defaultView;
win.Inspector_de_Info = window.Inspector_de_Info;
if (doc.getElementById("idei-container")) return;
var inspectFrame = doc.getElementById("toolbox-panel-iframe-inspector");
if (!inspectFrame)
return;
inspectFrame.addEventListener("load", inspectFrameOnload, true);
if (inspectFrame.contentDocument)
inspectFrameOnload(inspectFrame.contentDocument);
function inspectFrameOnload(iDoc) {
inspectFrame.removeEventListener("load", inspectFrameOnload, true);
var popup = iDoc.getElementById("inspector-node-popup");
var m = iDoc.createElement("menu");
m.setAttribute("label", "Get Selector");
m.setAttribute("id", "idei-selector-menu");
m.setAttribute("accesskey", "S");
var p = m.appendChild(iDoc.createElement("menupopup"));
p.setAttribute("onpopupshowing", "window.top.Inspector_de_Info.onPopupshowing(event)");
p.setAttribute("idei-type", "selector");
popup.insertBefore(m, popup.firstChild);
var m = iDoc.createElement("menu");
m.setAttribute("label", "Get XPath");
m.setAttribute("id", "idei-xpath-menu");
m.setAttribute("accesskey", "X");
var p = m.appendChild(iDoc.createElement("menupopup"));
p.setAttribute("onpopupshowing", "window.top.Inspector_de_Info.onPopupshowing(event)");
p.setAttribute("idei-type", "xpath");
popup.insertBefore(m, popup.firstChild);
};
var buttons = doc.getElementById("toolbox-buttons");
var infobutton = buttons.insertBefore(document.createElement("toolbarbutton"), buttons.firstChild);
infobutton.setAttribute("id", "idei-button");
infobutton.setAttribute("class", "command-button");
infobutton.setAttribute("image", "resource://gre/chrome/toolkit/skin/classic/mozapps/plugins/pluginHelp-16.png");
infobutton.setAttribute("label", "INFO");
infobutton.setAttribute("accesskey", "Q");
infobutton.addEventListener("command", function(event) {
var container = doc.getElementById("idei-container");
if (container.hidden = !container.hidden) return;
doc.getElementById("idei-url").value = "^" + content.location.href.replace(/[()\[\]{}|+.,^$?\\]/g, "\\$&");
}, false);
var xml = '\
<hbox id="idei-container" hidden="true">\
<popupset>\
<menupopup id="idei-textbox-popup">\
<menuitem label="@class="value" → contains()"\
tooltiptext="class 属性を contains() を使った形式にします"\
oncommand="Inspector_de_Info.replaceAttrXPath(Inspector_de_Info.NORMAL_TO_CLASS)" />\
<menuitem label="@name="value" → starts-with(@name, "value")"\
tooltiptext="属性を starts-with(@name, "value") にします"\
oncommand="Inspector_de_Info.replaceAttrXPath(Inspector_de_Info.NORMAL_TO_STARTS)" />\
<menuitem label="@name="value" → ends-with(@name, "value")"\
tooltiptext="属性を substring(@name, string-length(@name) - string-length("value") + 1) = "value" にします"\
oncommand="Inspector_de_Info.replaceAttrXPath(Inspector_de_Info.NORMAL_TO_ENDS)" />\
<menuseparator />\
<menuitem label="starts/ends/contains → @name="value""\
tooltiptext="属性を @name="value" に戻します"\
oncommand="Inspector_de_Info.replaceAttrXPath(Inspector_de_Info.SOME_TO_NORMAL)" />\
<menuseparator />\
<menuitem label=":first-child"\
tooltiptext="CSS の :first-child に相当する XPath を挿入します\n[not(preceding-sibling::*)]"\
oncommand="Inspector_de_Info.insertText(\'[not(preceding-sibling::*)]\');" />\
<menuitem label=":last-child"\
tooltiptext="CSS の :last-child に相当する XPath を挿入します\n[not(following-sibling::*)]"\
oncommand="Inspector_de_Info.insertText(\'[not(following-sibling::*)]\');" />\
<menuitem label=":only-child"\
tooltiptext="CSS の :only-child に相当する XPath を挿入します\n[count(parent::*/child::*) = 1]"\
oncommand="Inspector_de_Info.insertText(\'[count(parent::*/child::*) = 1]\');" />\
<menuitem label=":empty"\
tooltiptext="CSS の :empty に相当する XPath を挿入します\n[not(*) and not(text())]"\
oncommand="Inspector_de_Info.insertText(\'[not(*) and not(text())]\');" />\
<menuitem label="E の後の F ( E + F )"\
tooltiptext="CSS の E + F に相当する XPath を挿入します\nE/following-sibling::*[1][self::F]"\
oncommand="Inspector_de_Info.insertText(\'E/following-sibling::*[1][self::F]\');" />\
<menuitem label="E 以降の F ( E ~ F )"\
tooltiptext="CSS の E ~ F に相当する XPath を挿入します\nE/following-sibling::F"\
oncommand="Inspector_de_Info.insertText(\'E/following-sibling::F\');" />\
<menuitem label="E の前の F"\
tooltiptext="E/preceding-sibling::*[1][self::F]"\
oncommand="Inspector_de_Info.insertText(\'E/preceding-sibling::*[1][self::F]\');" />\
<menuitem label="E 以前の F"\
tooltiptext="E/preceding-sibling::F"\
oncommand="Inspector_de_Info.insertText(\'E/preceding-sibling::F\');" />\
</menupopup>\
</popupset>\
<vbox id="idei-hbox">\
<button label="JSON"\
oncommand="Inspector_de_Info.checkInfo(document, true);"/>\
<button label="launch"\
oncommand="Inspector_de_Info.launch(document);"\
tooltiptext="uAutoPagerize で実行してみます"/>\
</vbox>\
<grid id="idei-grid" flex="1">\
<columns>\
<column />\
<column flex="1"/>\
</columns>\
<rows>\
<row>\
<label value="url"/>\
<textbox id="idei-url"/>\
</row>\
<row>\
<label value="nextLink"/>\
<textbox id="idei-nextLink" context="idei-textbox-popup"/>\
</row>\
<row>\
<label value="pageElement"/>\
<textbox id="idei-pageElement" context="idei-textbox-popup"/>\
</row>\
<row>\
<label value="insertBefore"/>\
<textbox id="idei-insertBefore" context="idei-textbox-popup"/>\
</row>\
</rows>\
</grid>\
</hbox>\
';
var range = document.createRange();
range.selectNode(inspectFrame);
range.collapse(false);
range.insertNode(range.createContextualFragment(xml.replace(/\n|\t/g, '')));
},
init: function() {
gDevTools.on("toolbox-ready", Inspector_de_Info.ready);
window.addEventListener("unload", this, false);
},
uninit: function() {
window.removeEventListener("unload", this, false);
},
destroy: function() {
this.uninit();
["idei-button","idei-container","idei-selector-menu","idei-xpath-menu"].forEach(function(id){
var elem = $(id);
if (elem) elem.parentNode.removeChild(elem);
}, this);
if (this.xulstyle) this.xulstyle.parentNode.removeChild(this.xulstyle);
},
handleEvent: function(event) {
switch(event.type){
case "unload":
this.uninit();
break;
}
},
onPopupshowing: function (event) {
var popup = event.target;
var range = document.createRange();
range.selectNodeContents(popup);
range.deleteContents();
range.detach();
var elem = popup.ownerDocument.defaultView.inspector.selection.node;
if (elem instanceof Text) elem = elem.parentNode;
if (!(elem instanceof Element)) return;
var array = popup.getAttribute("idei-type") === "selector" ?
this.getSelectorAll(elem):
this.getXPathAll(elem);
this.createMenuitem(popup, array);
},
createMenuitem: function(popup, items) {
items.forEach(function(str, i, a) {
if (a.indexOf(str) !== i) return;
let m = document.createElement("menuitem");
m.setAttribute("label", str);
m.setAttribute("oncommand", "window.top.Inspector_de_Info.copyToClipboard(event)");
m.style.setProperty("max-width","63em","important");
if (str.length > 110)
m.setAttribute("tooltiptext", str);
popup.appendChild(m);
}, this);
},
run: function() {
var container = $("idei-container");
if (container.hidden = !container.hidden) return;
$("idei-url").value = "^" + InspectorUI.selection.ownerDocument.location.href.replace(/[()\[\]{}|+.,^$?\\]/g, "\\$&");
},
copyToClipboard: function(event) {
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper).copyString(event.target.getAttribute("label"));
},
getXPathAll: function(node) {
var res = [];
var localName = node.localName.toLowerCase();
var id = node.getAttribute('id');
if (id) res.push('id("' + id + '")');
var cls = node.getAttribute('class');
if (cls) {
res.push(localName + '[@class='+ escapeXPathExpr(cls) +']');
let cs = cls.trim().split(/\s+/).map(function(c) {
let contains = 'contains(concat(" ",normalize-space(@class)," "), " '+ c +' ")';
res.push(localName + '[' + contains + ']');
return contains;
}, this);
if (cs.length > 1)
res.push(localName + '[' + cs.join(' and ') + ']');
}
var attrs = [
[nodeName, nodeValue]
for each({nodeName, nodeValue} in $A(node.attributes))
if (!/^(?:id|class)$/i.test(nodeName))];
var all = attrs.map(function([n, v]){
if (n === 'href' || n === 'src') {
if (v.indexOf('javascript:') === 0) {
res.push(localName + '[starts-width(@'+ n +', "javascript:")]');
} else if (v.indexOf('data:') === 0) {
var str = (/^data\:.*?\,/.exec(v)||['data:'])[0];
res.push(localName + '[starts-width(@'+ n +', "'+ str +'")]');
}
}
res.push(localName + '[@'+ n +'='+ escapeXPathExpr(v) +']');
return '@'+ n +'='+ escapeXPathExpr(v) +'';
});
if (all.length > 1)
res.push(localName + '[' + all.join(' and ') + ']');
var first = node.firstChild;
if (first instanceof Text && /\S/.test(first.textContent))
res.push(localName + '[text()=' + escapeXPathExpr(first.textContent).replace(/\'/g, "\\\'") + ']')
res.push(localName);
return res;
},
getSelectorAll: function(node) {
var res = [];
var localName = node.localName.toLowerCase();
var id = node.getAttribute('id');
if (id) res.push('#' + id);
var cls = node.getAttribute('class');
if (cls) res.push('.' + Array.slice(node.classList).join('.'));
var attrs = [
[nodeName, nodeValue]
for each({nodeName, nodeValue} in $A(node.attributes))
if (!/^(?:id|class)$/i.test(nodeName))];
var all = attrs.map(function([n, v]){
if (n === 'href' || n === 'src') {
if (v.indexOf('data:') === 0) {
var str = (/^data\:.*?\,/.exec(v) || ['data:'])[0];
res.push(localName + '['+ n +'^="'+ str +'"]');
} else {
var str = (/[^/]+\/?$/.exec(v) || [""])[0];
var pre = v.slice(0, -str.length);
if (pre && pre != v) res.push(localName + '['+ n +'^="'+ pre +'"]');
if (str && str != v) res.push(localName + '['+ n +'$="'+ str +'"]');
}
}
res.push(localName + '['+ n +'="'+ v +'"]');
return '[' + n +'="'+ v +'"]';
});
if (all.length > 1)
res.push(localName + all.join(''));
res.push(localName);
return res;
},
toJSON: function(tDoc) {
var json = "{\n";
json += "\turl : '" + tDoc.getElementById("idei-url").value.replace(/\\/g, "\\\\") + "'\n";
json += "\t,nextLink : '" + tDoc.getElementById("idei-nextLink").value + "'\n";
json += "\t,pageElement : '" + tDoc.getElementById("idei-pageElement").value + "'\n";
if (tDoc.getElementById("idei-insertBefore").value)
json += "\t,insertBefore: '" + tDoc.getElementById("idei-insertBefore").value + "'\n";
json += "\t,exampleUrl : '" + content.location.href + "'\n";
json += "}";
// if (confirm(json + '\n\n設定ファイルに追記しますか?')) {
// this.addSaveFile('\nMY_SITEINFO.unshift('+ json +');');
// if (!window.uAutoPagerize) return
// uAutoPagerize.loadSetting(true);
// }
alert(json);
},
launch: function(tDoc) {
var uap = window.uAutoPagerize;
if (!uap) return this.checkInfo(true);
var win = content;
if (win.ap) return alert("既に実行されています");
var i = this.checkInfo(tDoc);
if (!i) return;
var [index, info] = uap.getInfo([i], win);
if (index === 0) {
if (win.AutoPagerize && win.AutoPagerize.launchAutoPager)
win.AutoPagerize.launchAutoPager([i]);
else alert("SITEINFO は正常ですが、uAutoPagerize は実行できませんでした");
} else {
alert("この SITEINFO にはマッチしませんでした");
}
},
addSaveFile: function(data) {
if (!window.uAutoPagerize)
return alert('uAutoPagerize がありません');
var file = uAutoPagerize.file;
if (!window.uAutoPagerize.file || !file.exists())
return alert('_uAutoPagerize.js が見つかりません');
var suConverter = Cc['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Ci.nsIScriptableUnicodeConverter);
suConverter.charset = 'UTF-8';
data = suConverter.ConvertFromUnicode(data);
var foStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x10, 0664, 0);
foStream.write(data, data.length);
foStream.close();
},
checkInfo: function(tDoc, isAlert) {
var i = {
url : tDoc.getElementById("idei-url").value,
nextLink : tDoc.getElementById("idei-nextLink").value,
pageElement : tDoc.getElementById("idei-pageElement").value,
insertBefore: tDoc.getElementById("idei-insertBefore").value
};
if (!i.url || !i.nextLink || !i.pageElement)
return alert("入力値が不正です");
var logs = [];
var win = content;
var doc = win.document;
try {
if (!new RegExp(i.url).test(doc.location.href))
logs.push("url がマッチしません");
} catch (e) {
return alert(e);
}
try {
if (!doc.evaluate(i.nextLink, doc, null, 9, null).singleNodeValue)
logs.push("nextLink が見つかりません");
} catch (e) {
logs.push("nextLink が不正です");
}
try {
if (!doc.evaluate(i.pageElement, doc, null, 9, null).singleNodeValue)
logs.push("pageElement が見つかりません");
} catch (e) {
logs.push("pageElement が不正です");
}
try {
if (i.insertBefore)
doc.evaluate(i.insertBefore, doc, null, 9, null).singleNodeValue;
} catch (e) {
logs.push("insertBefore が不正です");
}
if (logs.length)
return alert(logs.join("\n"));
if (isAlert)
this.toJSON(tDoc);
return i;
},
NORMAL_TO_CLASS: 0,
CLASS_TO_NORMAL: 1,
NORMAL_TO_STARTS: 2,
STARTS_TO_NOMARL: 3,
NORMAL_TO_ENDS: 4,
ENDS_TO_NORMAL: 5,
SOME_TO_NORMAL: 6,
replaceAttrXPath: function(constant) {
var t = document.commandDispatcher.focusedElement;
if (!t || !("selectionStart" in t)) return;
if (t.selectionStart == t.selectionEnd) {
t.select();
var value = t.value;
} else {
var value = t.value.substring(t.selectionStart, t.selectionEnd);
}
if (!value) return;
if (!t.editor || !t.editor.insertText) {
t instanceof Ci.nsIDOMNSEditableElement;
t.editor.QueryInterface(Ci.nsIPlaintextEditor);
}
switch (constant) {
case this.NORMAL_TO_CLASS : t.editor.insertText(normal2class(value)); break;
case this.CLASS_TO_NORMAL : t.editor.insertText(class2normal(value)); break;
case this.NORMAL_TO_STARTS: t.editor.insertText(normal2starts(value)); break;
case this.STARTS_TO_NOMARL: t.editor.insertText(starts2normal(value)); break;
case this.NORMAL_TO_ENDS : t.editor.insertText(normal2ends(value)); break;
case this.ENDS_TO_NORMAL : t.editor.insertText(ends2normal(value)); break;
case this.SOME_TO_NORMAL : t.editor.insertText(class2normal(starts2normal(ends2normal(value)))); break;
}
},
insertText: function(aStr) {
var cmd = "cmd_insertText";
var ctrl = document.commandDispatcher.getControllerForCommand(cmd);
if (!ctrl || !ctrl.isCommandEnabled(cmd)) return;
ctrl = ctrl.QueryInterface(Ci.nsICommandController);
var params = Cc["@mozilla.org/embedcomp/command-params;1"].createInstance(Ci.nsICommandParams);
params.setStringValue("state_data", aStr);
ctrl.doCommandWithParams(cmd, params);
},
};
window.Inspector_de_Info.init();
function normal2class(xpath) {
return xpath.replace(/@class=\"(.+?)\"/g, function(str, cls) {
cls = cls.replace(/\s+/g, " ").replace(/^\s+|\s+$/g, "").split(" ");
for (var i = 0, l = cls.length; i < l; i++) {
cls[i] = 'contains(concat(" ",normalize-space(@class)," "), " '+ cls[i] +' ")';
}
return cls.join(" and ");
});
}
function class2normal(xpath) {
let r = /(?:contains\(concat\(\" \"\,normalize\-space\(@class\)\,\" \"\)\, \" .+? \"\)(?: and )?)+/g;
return xpath.replace(r, function(str) {
let cls = str.split(' and ').map(function(c) c.replace(/.*\" (.*) \".*/i, '$1') );
return '@class="'+ cls.join(' ') +'"';
});
}
function normal2starts(xpath) {
return xpath.replace(/(@.+?)=\"(.+?)\"/g, function(str, aName, aValue) {
return 'starts-with('+ aName +', "'+ aValue +'")';
});
}
function starts2normal(xpath) {
return xpath.replace(/starts-with\((@.+?)\, \"(.+?)\"\)/g, function(str, aName, aValue) {
return aName + '="' + aValue + '"';
});
}
function normal2ends(xpath) {
return xpath.replace(/(@.+?)=\"(.+?)\"/g, function(str, aName, aValue) {
return 'substring('+ aName +', string-length(' + aName + ') - string-length("' + aValue + '") + 1) = "' + aValue + '"'
});
}
function ends2normal(xpath) {
return xpath.replace(/substring\((@.+?)\, string\-length\(@.+?\) \- string\-length\(\"(.+?)\"\) \+ 1\) = \".+?\"/g, function(str, aName, aValue) {
return aName + '="' + aValue + '"';
});
}
function $(id) { return document.getElementById(id); }
function $$(exp, doc) { return Array.prototype.slice.call((doc || document).querySelectorAll(exp)); }
function $A(args) { return Array.prototype.slice.call(args); }
function U(text) 1 < 'あ'.length ? decodeURIComponent(escape(text)) : text;
function $C(name, attr) {
var el = document.createElement(name);
if (attr) Object.keys(attr).forEach(function(n) el.setAttribute(n, attr[n]));
return el;
}
function addStyle(css) {
var pi = document.createProcessingInstruction(
'xml-stylesheet',
'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(css) + '"'
);
return document.insertBefore(pi, document.documentElement);
}
// http://d.hatena.ne.jp/amachang/20090917/1253179486
function escapeXPathExpr(text) {
var matches = text.match(/[^"]+|"/g);
function esc(t) {
return t == '"' ? ('\'' + t + '\'') : ('"' + t + '"');
}
if (matches) {
if (matches.length == 1) {
return esc(matches[0]);
} else {
var results = [];
for (var i = 0, len = matches.length; i < len; i ++) {
results.push(esc(matches[i]));
}
return 'concat(' + results.join(', ') + ')';
}
} else {
return '""';
}
}
})('\
#inspector-toolbar[hidden="true"] ~ #idei-container\
{ display: none !important; }\
\
#idei-container row {\
-moz-box-align: center !important;\
}\
#idei-container label {\
margin: 0px !important;\
}\
#idei-container textbox {\
margin: 1px !important;\
}\
\
');