aboutsummaryrefslogtreecommitdiffstats
path: root/inspector.js
blob: 3d882b5485ac1502331ff41d7758e2ab6952c3c3 (plain)
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
/**
 * ==VimperatorPlugin==
 * @name          inspector
 * @description   DOM Inspector commands
 * @depend        "DOM Inspector" inspector@mozilla.org
 * @author        teramako teramako@gmail.com
 * @minVersion    2.0pre
 * @version       1.1
 * ==/VimperatorPlugin==
 *
 * Usage:
 * 
 * :inspect #{id}             -> inspect the element of the {id} in browser content
 * :inspect! #{id}            -> inspect the element of the {id} in firefox
 * :inspect[!] -f[rame] #{id} -> inspect the document in the frame element of the {id}
 * :inspect {str}             -> inspect the return value of evaluated the {str}
 */

(function(){

const inspectorID = "inspector@mozilla.org";
if (!Application.extensions.has(inspectorID) || !Application.extensions.get(inspectorID).enabled) return;

/* これやるとFirefox終了時に実行されるんだけど...なぜ? -> Ubiquityが悪さしているみたい
Object.prototype.inspect = function(){
	runInspector(this);
};
*/

function runInspector(node){
	if (node instanceof Document){
		inspectDOMDocument(node);
	} else if (node instanceof Node){
		inspectDOMNode(node);
	} else if (node !== null && typeof node != "undefined"){
		inspectObject(node);
	}
}

function getIDList(filter, isChrome){
	var doc = isChrome ? document : content.document;
	var iter = buffer.evaluateXPath('//*[@id and contains(@id,"' + filter + '")]',doc);
	return [["#" + e.id, "TagName: "+ e.tagName] for (e in iter)];
}

var options = [
	[["-frame","-f"], commands.OPTION_NOARG]
];
commands.addUserCommand(["inspect","dominspect"],"run DOM Inspector",
	function(args){
		var arg = args[0];
		var doc = args.bang ? document : content.document;
		var node;
		if (!arg){
			node = doc;
		} else if (arg.charAt(0) == "#"){
			let id = arg.substr(1);
			node = doc.getElementById(id);
			if (!node){
				liberator.echoerr("No such id: " + id );
				return;
			}
		} else {
			try {
				node = liberator.eval(args.string);
			} catch (e){
				liberator.echoerr(e);
			}
		}
		if (args["-frame"] && node.contentDocument) node = node.contentDocument;
		runInspector(node);
	},{
		bang: true,
		argCount: "*",
		options: options,
		completer: function(context, args){
			if (args[0] && args[0].charAt(0) == "#"){
				var arg = args[0];
				var list = getIDList(arg.substr(1), args.bang);
				context.completions = list.filter(function(elem) elem[0].indexOf(arg) == 0);
			} else {
				completion.javascript(context);
			}
		}
	}
);

})();

span class="p">, ' ' + script.includes.join('\n '), '<span style="font-weight:bold;">excludes</span>:', ' ' + script.excludes.join('\n '), '<span style="font-weight:bold;">enabled</span>: ' + script.enabled ].join('\n'); } } ); commands.addUserCommand(['gmlo[ad]'], 'load Greasemonkey script', function(arg){ if (!arg) return; var scripts = getScripts(); var script; for (var i=0; i<scripts.length; i++){ if (scripts[i].filename == arg || scripts[i].name == arg){ script = scripts[i]; break; } } if (!script) { echoerr('Usage: :gmlo[ad] {name|filename}'); return; } else { echo('load: ' +script.filename); } try { var href = vimperator.buffer.URL; var unsafewin = window.content.document.defaultView.wrappedJSObject; GM_BrowserUI.gmSvc.wrappedJSObject.injectScripts([script],href,unsafewin,window); } catch(e){ log(e); } /* // do you have idea how to dispatch load event to only the script ? window.setTimeout(function(){ var loadEvent = document.createEvent('Event'); loadEvent.initEvent('load',true,true, window.content.document,1); window.content.document.dispatchEvent(loadEvent); },100); */ },{ args: ['{name|filename}'], completer: function(filter){ return scriptsCompleter(filter,true); } } ); commands.addUserCommand(['gmset'], 'change setting a greasemonkey script', function(arg, special){ var res = liberator.commands.parseArgs(arg, this.args); if (!res) { if (special) GM_setEnabled(!GM_getEnabled()); // toggle enable/disable greasemonkey return; } var filename = res.args[0]; var config = new Config(); config.load(); var script; for (var i=0; i<config.scripts.length; i++){ if (config.scripts[i].filename == filename){ script = config.scripts[i]; break; } } if (!script) return; if (special){ // toggle enable/disable the script if {filename} is exist script.enabled = !script.enabled; } if (res.opts.length > 0){ script.name = liberator.commands.getOption(res.opts, '-name', script.name); script.includes = liberator.commands.getOption(res.opts, '-include', script.includes); script.excludes = liberator.commands.getOption(res.opts, '-exclude', script.excludes); } config.save(); },{ args: [ [['-name','-n'], liberator.commands.OPTION_STRING], [['-include','-i'], liberator.commands.OPTION_LIST], [['-exclude','-e'], liberator.commands.OPTION_LIST] ], shortHelp: 'change setting a greasemonkey script', help: [ 'toggle enable/disable with "!", if <code>{filename}</code> is exist, if not toggle greasemonkey', '<dl><dt>-n<br/>-name</dt><dd>change the name</dd>', '<dt>-i<br/>-include</dt><dd>change the inclue list ("," demiliter)</dd>', '<dt>-e<br/>-exclude</dt><dd>change the exclude list ("," demiliter)</dd></dl>', 'Caution: the change is permanent, not the only session.<br/>And cannot get back.' ].join(''), completer: function(filter){ return scriptsCompleter(filter, false); } } ); function getScripts(){ var config = new Config(); config.load(); return config.scripts; } function scriptsCompleter(filter,flag){ var candidates = []; var scripts = getScripts(); var isAll = false; if (!filter) isAll=true; if (flag){ for (var i=0; i<scripts.length; i++){ if (isAll || scripts[i].name.toLowerCase().indexOf(filter) == 0 || scripts[i].filename.indexOf(filter) == 0) { candidates.push([scripts[i].name, scripts[i].description]); candidates.push([scripts[i].filename, scripts[i].description]); } } } else { for (var i=0; i<scripts.length; i++){ if (isAll || scripts[i].filename.indexOf(filter) == 0) { candidates.push([scripts[i].filename, scripts[i].description]); } } } return [0,candidates]; } })(); // vim: set fdm=marker sw=4 ts=4 et: