OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * A bootstrapped extension must load its own command-line handler, because it w
on't load from chrome.manifest. See |
| 3 * https://developer.mozilla.org/en-US/docs/Chrome_Registration and its section
"Instructions supported in bootstrapped |
| 4 * add-ons". This is only barely mentioned in |
| 5 * https://developer.mozilla.org/en-US/docs/Extensions/Bootstrapped_extensions,
and only in passing at that. The upshot |
| 6 * is that you have to replicate what would otherwise happen in chrome.manifest. |
| 7 * |
| 8 * A command line handler must implement the interface nsICommandLineHandler and
, in order to hook into the command line |
| 9 * system, must register itself in the category "command-line-handler". See |
| 10 * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsICommand
LineHandler |
| 11 * The value of the category entry is a service contract ID, which is used to co
nstruct an instance of the handler. |
| 12 * |
| 13 * The way to construct an instance given a contract ID is to implement nsIFacto
ry and, in order that hook into the |
| 14 * instantiation system, must register itself with the component registrar. The
component registrar is also the |
| 15 * component manager, but through the interface nsIComponentRegistrar, which is
not the default interface. |
| 16 */ |
| 17 |
| 18 Cu.import( "resource://gre/modules/XPCOMUtils.jsm" ); |
| 19 |
| 20 let { Bootstrap_XPCOM } = require( "bootstrap_xpcom" ); |
| 21 |
| 22 //------------------------------------------------------------------------------
----------- |
| 23 // Command_Line |
| 24 //------------------------------------------------------------------------------
----------- |
| 25 |
| 26 /** |
| 27 * The command line handler singleton. |
| 28 * |
| 29 * This object supplies its own factory to XPCOM, so there's no need for a const
ructor function; instead, the factory |
| 30 * function simply returns 'this' (essentially, though it passes it through Quer
yInterface first). |
| 31 */ |
| 32 var Command_Line = new Bootstrap_XPCOM.Singleton_class( |
| 33 "ABP Crawler - Command Line Handler", |
| 34 Components.ID( "{771575E6-62FE-48CB-BC24-EAEFDDC1CA1D}" ), |
| 35 "@adblockplus.org/abpcrawler/command-line;1", |
| 36 [ Ci.nsICommandLineHandler ], |
| 37 [ |
| 38 { |
| 39 category: "command-line-handler", |
| 40 // The entry starts with "k" so that it has slightly higher priority than
ordinary command line handlers. |
| 41 entry: "k-abpcrawler" |
| 42 } |
| 43 ] ); |
| 44 |
| 45 Command_Line.helpInfo = "" + |
| 46 // - - - - - - - - - - - - - - | wrap
here |
| 47 "AdBlock Plus Crawler\n" + |
| 48 " -abpcrawler Start a crawl. Must specify both an input and\n" + |
| 49 " an output.\n" + |
| 50 " -input_file <path> Use <path> as input file from which to compile\n" + |
| 51 " instructions for the crawl.\n" + |
| 52 " -output_dir <path> Use <path> as output directory to contain a\n" + |
| 53 " file of crawl results.\n" + |
| 54 " -output_base <name> Use <name> as the base name for a file of crawl\n" + |
| 55 " results. Optional. Default='crawl_results'.\n" + |
| 56 " -max_tabs <N> Maximum number of tabs for simultananeous\n" + |
| 57 " loading of target sites.\n"; |
| 58 |
| 59 /** |
| 60 * Set a startup hook to run |
| 61 * @param f |
| 62 */ |
| 63 Command_Line.set_startup_hook = function( f ) |
| 64 { |
| 65 this._startup_hook = f; |
| 66 }; |
| 67 |
| 68 /** |
| 69 * The actual handler. |
| 70 * |
| 71 * @param {nsICommandLine} ff_command_line |
| 72 * The Firefox command line |
| 73 */ |
| 74 Command_Line.handle = function( ff_command_line ) |
| 75 { |
| 76 dump( "Command line handler: abpcrawler\n" ); |
| 77 |
| 78 if ( !ff_command_line.handleFlag( "abpcrawler", false ) ) |
| 79 { |
| 80 /* |
| 81 * There's no '--abpcrawler' option on the command line. As a result we don'
t try to interpret any other |
| 82 * command line flags. |
| 83 */ |
| 84 return; |
| 85 } |
| 86 var flags = this.flags = { abpcrawler: true }; |
| 87 /* |
| 88 * The '--abpcrawler' argument indicates that this invocation is designated as
a crawl session. As a result, |
| 89 * we don't perform the default action, which is opening the start page in the
browser for interactive use. |
| 90 */ |
| 91 ff_command_line.preventDefault = true; |
| 92 |
| 93 let x = ff_command_line.handleFlagWithParam( "input_file", false ); |
| 94 if ( x ) flags.input_file = x; |
| 95 x = ff_command_line.handleFlagWithParam( "output_dir", false ); |
| 96 if ( x ) flags.output_dir = x; |
| 97 x = ff_command_line.handleFlagWithParam( "output_base", false ); |
| 98 if ( x ) flags.output_base = x; |
| 99 x = ff_command_line.handleFlagWithParam( "max_tabs", false ); |
| 100 if ( x ) flags.max_tabs = x; |
| 101 |
| 102 /* |
| 103 * Some experimentation revealed that this command line handler runs _after_ o
bserver notifications for topic |
| 104 * 'final-ui-startup'. (This seems like a design failure, since it ought to ha
ppen after the add-on has an opportunity |
| 105 * to initialize itself at 'profile-after-change' but before anything else. Wh
atever.) As a result, we call a startup |
| 106 * hook here. |
| 107 */ |
| 108 if ( this._startup_hook ) |
| 109 { |
| 110 try |
| 111 { |
| 112 this._startup_hook(); |
| 113 } |
| 114 catch ( e ) |
| 115 { |
| 116 dump( "Command_Line/handle/startup hook: Unexpected exception" |
| 117 + (("message" in e) ? ": " + e.message : "") + ".\n" ); |
| 118 } |
| 119 } |
| 120 }; |
| 121 |
| 122 exports.Command_Line = Command_Line; |
| 123 try |
| 124 { |
| 125 /* |
| 126 * We must call init() after handle() is defined, so that if the category mana
ger triggers an immediate event |
| 127 * that we have already initialized fully. |
| 128 */ |
| 129 Command_Line.init(); |
| 130 } |
| 131 catch ( e ) |
| 132 { |
| 133 dump( "command_line.js: Unexpected exception during init(): " + e.message ); |
| 134 } |
OLD | NEW |