OLD | NEW |
1 // Implementation of the Greasemonkey API, see: | 1 // Implementation of the Greasemonkey API, see: |
2 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs | 2 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs |
3 | 3 |
4 const MIN_INT_32 = -0x80000000; | 4 const MIN_INT_32 = -0x80000000; |
5 const MAX_INT_32 = 0x7FFFFFFF; | 5 const MAX_INT_32 = 0x7FFFFFFF; |
6 | 6 |
7 // Prefix for user script values that are stored in localStorage. | 7 // Prefix for user script values that are stored in localStorage. |
8 const STORAGE_NS = "__userscript__."; | 8 const STORAGE_NS = "__userscript__."; |
9 | 9 |
10 function GM_getValue(name, defaultValue) { | 10 function GM_getValue(name, defaultValue) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 function GM_getResourceURL(resourceName) { | 57 function GM_getResourceURL(resourceName) { |
58 throw new Error("not implemented."); | 58 throw new Error("not implemented."); |
59 } | 59 } |
60 | 60 |
61 function GM_getResourceText(resourceName) { | 61 function GM_getResourceText(resourceName) { |
62 throw new Error("not implemented."); | 62 throw new Error("not implemented."); |
63 } | 63 } |
64 | 64 |
65 function GM_addStyle(css) { | 65 function GM_addStyle(css) { |
66 var head = document.getElementsByTagName("head")[0]; | 66 var parent = document.getElementsByTagName("head")[0]; |
67 if (!head) { | 67 if (!parent) { |
68 return; | 68 parent = document.documentElement; |
69 } | 69 } |
70 var style = document.createElement("style"); | 70 var style = document.createElement("style"); |
71 style.type = "text/css"; | 71 style.type = "text/css"; |
72 style.innerHTML = css; | 72 var textNode = document.createTextNode(css); |
73 head.appendChild(style); | 73 style.appendChild(textNode); |
| 74 parent.appendChild(style); |
74 } | 75 } |
75 | 76 |
76 function GM_xmlhttpRequest(details) { | 77 function GM_xmlhttpRequest(details) { |
77 throw new Error("not implemented."); | 78 function setupEvent(xhr, url, eventName, callback) { |
| 79 xhr[eventName] = function () { |
| 80 var isComplete = xhr.readyState == 4; |
| 81 var responseState = { |
| 82 responseText: xhr.responseText, |
| 83 readyState: xhr.readyState, |
| 84 responseHeaders: isComplete ? xhr.getAllResponseHeaders() : "", |
| 85 status: isComplete ? xhr.status : 0, |
| 86 statusText: isComplete ? xhr.statusText : "", |
| 87 finalUrl: isComplete ? url : "" |
| 88 }; |
| 89 callback(responseState); |
| 90 }; |
| 91 } |
| 92 |
| 93 var xhr = new XMLHttpRequest(); |
| 94 var eventNames = ["onload", "onerror", "onreadystatechange"]; |
| 95 for (var i = 0; i < eventNames.length; i++ ) { |
| 96 var eventName = eventNames[i]; |
| 97 if (eventName in details) { |
| 98 setupEvent(xhr, details.url, eventName, details[eventName]); |
| 99 } |
| 100 } |
| 101 |
| 102 xhr.open(details.method, details.url); |
| 103 |
| 104 if (details.overrideMimeType) { |
| 105 xhr.overrideMimeType(details.overrideMimeType); |
| 106 } |
| 107 if (details.headers) { |
| 108 for (var header in details.headers) { |
| 109 xhr.setRequestHeader(header, details.headers[header]); |
| 110 } |
| 111 } |
| 112 xhr.send(details.data ? details.data : null); |
78 } | 113 } |
79 | 114 |
80 function GM_registerMenuCommand(commandName, commandFunc, accelKey, | 115 function GM_registerMenuCommand(commandName, commandFunc, accelKey, |
81 accelModifiers, accessKey) { | 116 accelModifiers, accessKey) { |
82 throw new Error("not implemented."); | 117 throw new Error("not implemented."); |
83 } | 118 } |
84 | 119 |
85 function GM_openInTab(url) { | 120 function GM_openInTab(url) { |
86 window.open(url, ""); | 121 window.open(url, ""); |
87 } | 122 } |
88 | 123 |
89 function GM_log(message) { | 124 function GM_log(message) { |
90 window.console.log(message); | 125 window.console.log(message); |
91 } | 126 } |
OLD | NEW |