1 // Copyright 2014 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.
5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect.
8 // -----------------------------------------------------------------------------
10 // Partial implementation of the Greasemonkey API, see:
11 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs
13 function GM_addStyle(css) {
14 var parent = document.getElementsByTagName("head")[0];
16 parent = document.documentElement;
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);
25 function GM_xmlhttpRequest(details) {
26 function setupEvent(xhr, url, eventName, callback) {
27 xhr[eventName] = function () {
28 var isComplete = xhr.readyState == 4;
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 : ""
37 callback(responseState);
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]);
50 xhr.open(details.method, details.url);
52 if (details.overrideMimeType) {
53 xhr.overrideMimeType(details.overrideMimeType);
55 if (details.headers) {
56 for (var header in details.headers) {
57 xhr.setRequestHeader(header, details.headers[header]);
60 xhr.send(details.data ? details.data : null);
63 function GM_openInTab(url) {
67 function GM_log(message) {
68 window.console.log(message);
72 function generateGreasemonkeyStub(name) {
74 console.log("%s is not supported.", name);
78 var apis = ["GM_getValue", "GM_setValue", "GM_registerMenuCommand"];
79 for (var i = 0, api; api = apis[i]; i++) {
80 window[api] = generateGreasemonkeyStub(api);