| 1 | /** |
|---|
| 2 | * The Fuzz Example extension Javascript functionality for the |
|---|
| 3 | * Firefox web browser. |
|---|
| 4 | * |
|---|
| 5 | * This is an example Firefox plug-in that uses the Fuzz Firefox |
|---|
| 6 | * plugin to perform triple discovery in web pages. This plugin |
|---|
| 7 | * then takes the discovered triples and displays them in a small |
|---|
| 8 | * window when the "Fe" icon is clicked in the Firefox status |
|---|
| 9 | * bar at the bottom of the window. |
|---|
| 10 | * |
|---|
| 11 | * This extension is provided as a starting point for other |
|---|
| 12 | * Fuzz extension writers and for anybody that would like to use |
|---|
| 13 | * the power of RDFa in their Firefox extensions without having |
|---|
| 14 | * to write an RDFa parser from scratch. |
|---|
| 15 | * |
|---|
| 16 | * Fuzz uses a very fast and powerful RDFa parser called librdfa, |
|---|
| 17 | * which is capable of processing more than 8,000 triples per |
|---|
| 18 | * second (which is over 80MB of data per second) on an Athlon |
|---|
| 19 | * XP 1800+ processor. |
|---|
| 20 | * |
|---|
| 21 | * @author Manu Sporny |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /* The triple store contains all of the triples on the current page. */ |
|---|
| 25 | var gFuzzExampleTripleStore = {}; |
|---|
| 26 | |
|---|
| 27 | /* The number of triples in the current document. */ |
|---|
| 28 | var gFuzzExampleNumTriples = 0; |
|---|
| 29 | |
|---|
| 30 | /* The global triple UI reference */ |
|---|
| 31 | var gFuzzExampleTripleUi = null; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Logs a message to the Firefox error console, which can be accessed by |
|---|
| 35 | * typing CTRL+SHIFT+J. |
|---|
| 36 | * |
|---|
| 37 | * @param msg the message to log to the Firefox error console. |
|---|
| 38 | */ |
|---|
| 39 | function _fuzzExampleLog(msg) |
|---|
| 40 | { |
|---|
| 41 | Components |
|---|
| 42 | .classes["@mozilla.org/consoleservice;1"] |
|---|
| 43 | .getService(Components.interfaces["nsIConsoleService"]) |
|---|
| 44 | .logStringMessage(msg); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Updates the Firefox status bar icon with a colored version of the icon |
|---|
| 49 | * if triples are available, or a gray versiono f the icon if no triples |
|---|
| 50 | * were discovered. |
|---|
| 51 | */ |
|---|
| 52 | function fuzzExampleUpdateStatusDisplay() |
|---|
| 53 | { |
|---|
| 54 | var statusImage = document.getElementById("fuzz-example-status-image"); |
|---|
| 55 | |
|---|
| 56 | // update the status bar image icon |
|---|
| 57 | if(gFuzzExampleNumTriples > 0) |
|---|
| 58 | { |
|---|
| 59 | statusImage.src = |
|---|
| 60 | "chrome://fuzz-example/content/fuzz-example16-online.png"; |
|---|
| 61 | } |
|---|
| 62 | else |
|---|
| 63 | { |
|---|
| 64 | statusImage.src = |
|---|
| 65 | "chrome://fuzz-example/content/fuzz-example16-offline.png"; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * This function is called by the FuzzTripleObserver whenever a triple |
|---|
| 71 | * is detected. The data passed to the function must contain a subject, |
|---|
| 72 | * predicate, object, language and datatype. |
|---|
| 73 | * |
|---|
| 74 | * @param data the triple that was discovered, which must contain a |
|---|
| 75 | * subject, predicate, object, language and datatype. |
|---|
| 76 | */ |
|---|
| 77 | function fuzzExampleTripleHandler(data) |
|---|
| 78 | { |
|---|
| 79 | // don't count the triple if it is a prefix |
|---|
| 80 | if(data.subject != "@prefix") |
|---|
| 81 | { |
|---|
| 82 | gFuzzExampleNumTriples += 1; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | // strip any whitespace from the object literal |
|---|
| 86 | var strippedObject = data.object.replace(/\r/g, " "); |
|---|
| 87 | strippedObject = strippedObject.replace(/\n/g, " "); |
|---|
| 88 | strippedObject = strippedObject.replace(/\t/g, " "); |
|---|
| 89 | strippedObject = strippedObject.replace(/ +/g, " "); |
|---|
| 90 | strippedObject = strippedObject.replace(/^ +/g, ""); |
|---|
| 91 | |
|---|
| 92 | // create a new triple object |
|---|
| 93 | triple = new Object(); |
|---|
| 94 | triple.subject = data.subject; |
|---|
| 95 | triple.predicate = data.predicate; |
|---|
| 96 | triple.object = strippedObject; |
|---|
| 97 | triple.language = data.language; |
|---|
| 98 | triple.datatype = data.datatype; |
|---|
| 99 | |
|---|
| 100 | // if the subject doesn't exist in the triple store, create it |
|---|
| 101 | if(!gFuzzExampleTripleStore[data.subject]) |
|---|
| 102 | { |
|---|
| 103 | gFuzzExampleTripleStore[data.subject] = new Array(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | gFuzzExampleTripleStore[data.subject].push(triple); |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Updates the list of triples in the FuzzExample triple UI. At the moment |
|---|
| 111 | * the formatting and display of the data is very simplistic - just a long |
|---|
| 112 | * list of triples in a text area. |
|---|
| 113 | */ |
|---|
| 114 | function fuzzExampleUpdateTripleUi() |
|---|
| 115 | { |
|---|
| 116 | var triples = ""; |
|---|
| 117 | |
|---|
| 118 | // Build the triple text |
|---|
| 119 | for(var i in gFuzzExampleTripleStore) |
|---|
| 120 | { |
|---|
| 121 | for(var j in gFuzzExampleTripleStore[i]) |
|---|
| 122 | { |
|---|
| 123 | var triple = gFuzzExampleTripleStore[i][j]; |
|---|
| 124 | var subject = triple.subject; |
|---|
| 125 | var predicate = triple.predicate; |
|---|
| 126 | var object = triple.object; |
|---|
| 127 | |
|---|
| 128 | // surround any URL with angle brackets |
|---|
| 129 | if(subject.indexOf("http://") == 0) |
|---|
| 130 | { |
|---|
| 131 | subject = "<" + subject + ">"; |
|---|
| 132 | } |
|---|
| 133 | if(predicate.indexOf("http://") == 0) |
|---|
| 134 | { |
|---|
| 135 | predicate = "<" + predicate + ">"; |
|---|
| 136 | } |
|---|
| 137 | if(object.indexOf("http://") == 0) |
|---|
| 138 | { |
|---|
| 139 | object = "<" + object + ">"; |
|---|
| 140 | } |
|---|
| 141 | else |
|---|
| 142 | { |
|---|
| 143 | object = "\"" + object + "\""; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | // format @prefix data differently from non-@prefix data |
|---|
| 147 | if(subject != "@prefix") |
|---|
| 148 | { |
|---|
| 149 | triples = triples + subject + "\n " + predicate + "\n " + |
|---|
| 150 | object + " .\n\n"; |
|---|
| 151 | } |
|---|
| 152 | else |
|---|
| 153 | { |
|---|
| 154 | triples = |
|---|
| 155 | triples + subject + " " + predicate + " " + object + " .\n\n"; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // set the triple text for the text box |
|---|
| 161 | if(gFuzzExampleTripleUi != null) |
|---|
| 162 | { |
|---|
| 163 | var textBox = gFuzzExampleTripleUi.document.getElementById( |
|---|
| 164 | "fuzz-example-triple-textbox"); |
|---|
| 165 | textBox.value = triples; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Hides or displays the FuzzExample UI. |
|---|
| 171 | */ |
|---|
| 172 | function fuzzExampleToggleTripleUi() |
|---|
| 173 | { |
|---|
| 174 | // if the triple UI has not been created, create it |
|---|
| 175 | // if it has already been created, destroy it |
|---|
| 176 | if(gFuzzExampleTripleUi == null) |
|---|
| 177 | { |
|---|
| 178 | gFuzzExampleTripleUi = window.openDialog( |
|---|
| 179 | "chrome://fuzz-example/content/fuzz_example_triple_display.xul", |
|---|
| 180 | "Fuzz Example Triple Display", "chrome,centerscreen"); |
|---|
| 181 | gFuzzExampleTripleUi.ondialogaccept = fuzzExampleToggleTripleUi; |
|---|
| 182 | gFuzzExampleTripleUi.onload = fuzzExampleUpdateTripleUi; |
|---|
| 183 | } |
|---|
| 184 | else |
|---|
| 185 | { |
|---|
| 186 | gFuzzExampleTripleUi.close(); |
|---|
| 187 | gFuzzExampleTripleUi = null; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /** |
|---|
| 192 | * Clears all of the triples in the local triple store. |
|---|
| 193 | */ |
|---|
| 194 | function fuzzExampleClearTriples() |
|---|
| 195 | { |
|---|
| 196 | // re-initialize the triple-store |
|---|
| 197 | gFuzzExampleTripleStore = {}; |
|---|
| 198 | gFuzzExampleNumTriples = 0; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * The Fuzz Triple Observer is used to listen to RDF triple events created by |
|---|
| 203 | * the Fuzz Firefox Add-on. The triple observer is registered with the Fuzz |
|---|
| 204 | * extension and the "observe" function is called whenever there is a |
|---|
| 205 | * Fuzz-based triple event. |
|---|
| 206 | */ |
|---|
| 207 | FuzzExampleTripleObserver = |
|---|
| 208 | { |
|---|
| 209 | /** |
|---|
| 210 | * The observer function is called whenever there is a Fuzz-based triple |
|---|
| 211 | * event. |
|---|
| 212 | * |
|---|
| 213 | * @param topic The topic of the event, which is a text string describing |
|---|
| 214 | * the event. |
|---|
| 215 | * @param data The data associated with the event, usually a triple. |
|---|
| 216 | */ |
|---|
| 217 | observe: function(topic, data) |
|---|
| 218 | { |
|---|
| 219 | if(topic == "fuzz-triple-detected") |
|---|
| 220 | { |
|---|
| 221 | fuzzExampleTripleHandler(data); |
|---|
| 222 | } |
|---|
| 223 | else if(topic == "fuzz-triple-extraction-start") |
|---|
| 224 | { |
|---|
| 225 | fuzzExampleClearTriples(); |
|---|
| 226 | fuzzExampleUpdateStatusDisplay(); |
|---|
| 227 | } |
|---|
| 228 | else if(topic == "fuzz-triple-extraction-complete") |
|---|
| 229 | { |
|---|
| 230 | fuzzExampleUpdateStatusDisplay(); |
|---|
| 231 | } |
|---|
| 232 | else if(topic == "fuzz-triple-warning") |
|---|
| 233 | { |
|---|
| 234 | _fuzzExampleLog("fuzz-triple-warning not implemented!"); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | }; |
|---|
| 238 | |
|---|
| 239 | // register the Fuzz triple observer |
|---|
| 240 | Fuzz.addObserver(FuzzExampleTripleObserver); |
|---|