1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // ----------------------------------------------------------------------------- 6 // NOTE: If you change this file you need to touch renderer_resources.grd to 7 // have your change take effect. 8 // ----------------------------------------------------------------------------- 9 10 // Partial implementation of the Greasemonkey API, see: 11 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs 12 13 function GM_addStyle(css) { 14 var parent = document.getElementsByTagName("head")[0]; 15 if (!parent) { 16 parent = document.documentElement; 17 } 18 var style = document.createElement("style"); 19 style.type = "text/css"; 20 var textNode = document.createTextNode(css); 21 style.appendChild(textNode); 22 parent.appendChild(style); 23 } 24 25 function GM_xmlhttpRequest(details) { 26 function setupEvent(xhr, url, eventName, callback) { 27 xhr[eventName] = function () { 28 var isComplete = xhr.readyState == 4; 29 var responseState = { 30 responseText: xhr.responseText, 31 readyState: xhr.readyState, 32 responseHeaders: isComplete ? xhr.getAllResponseHeaders() : "", 33 status: isComplete ? xhr.status : 0, 34 statusText: isComplete ? xhr.statusText : "", 35 finalUrl: isComplete ? url : "" 36 }; 37 callback(responseState); 38 }; 39 } 40 41 var xhr = new XMLHttpRequest(); 42 var eventNames = ["onload", "onerror", "onreadystatechange"]; 43 for (var i = 0; i < eventNames.length; i++ ) { 44 var eventName = eventNames[i]; 45 if (eventName in details) { 46 setupEvent(xhr, details.url, eventName, details[eventName]); 47 } 48 } 49 50 xhr.open(details.method, details.url); 51 52 if (details.overrideMimeType) { 53 xhr.overrideMimeType(details.overrideMimeType); 54 } 55 if (details.headers) { 56 for (var header in details.headers) { 57 xhr.setRequestHeader(header, details.headers[header]); 58 } 59 } 60 xhr.send(details.data ? details.data : null); 61 } 62 63 function GM_openInTab(url) { 64 window.open(url, ""); 65 } 66 67 function GM_log(message) { 68 window.console.log(message); 69 } 70 71 (function() { 72 function generateGreasemonkeyStub(name) { 73 return function() { 74 console.log("%s is not supported.", name); 75 }; 76 } 77 78 var apis = ["GM_getValue", "GM_setValue", "GM_registerMenuCommand"]; 79 for (var i = 0, api; api = apis[i]; i++) { 80 window[api] = generateGreasemonkeyStub(api); 81 } 82 })(); 83