Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/renderer/resources/greasemonkey_api.js

Issue 149619: Various minor extension fixes (Closed)
Patch Set: One more test Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ----------------------------------------------------------------------------- 5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to 6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect. 7 // have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 // Implementation of the Greasemonkey API, see: 10 // Partial implementation of the Greasemonkey API, see:
11 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs 11 // http://wiki.greasespot.net/Greasemonkey_Manual:APIs
12 12
13 const MIN_INT_32 = -0x80000000;
14 const MAX_INT_32 = 0x7FFFFFFF;
15
16 // Prefix for user script values that are stored in localStorage.
17 const STORAGE_NS = "__userscript__.";
18
19 function GM_getValue(name, defaultValue) {
20 var value = localStorage.getItem(STORAGE_NS + name);
21 return value ? value : defaultValue;
22 }
23
24 function GM_setValue(name, value) {
25 // The values for GM_getValue() and GM_setValue() can only be boolean,
26 // strings, or 32 bit integers. See the setPrefs function in:
27 // http://greasemonkey.devjavu.com/browser/trunk/src/chrome/chromeFiles/conten t/prefmanager.js
28 var goodType = false;
29 switch (typeof(value)) {
30 case "string":
31 case "boolean":
32 goodType = true;
33 break;
34 case "number":
35 // Note that "value % 1 == 0" checks that the number is not a float.
36 if (value % 1 == 0 && value >= MIN_INT_32 && value <= MAX_INT_32) {
37 goodType = true;
38 }
39 break;
40 }
41
42 if (!goodType) {
43 throw new Error("Unsupported type for GM_setValue. Supported types " +
44 "are: string, bool, and 32 bit integers.");
45 }
46
47 localStorage.setItem(STORAGE_NS + name, value);
48 }
49
50 function GM_deleteValue(name) {
51 localStorage.removeItem(STORAGE_NS + name);
52 }
53
54 function GM_listValues() {
55 var values = [];
56 for (var i = 0; i < localStorage.length; i++) {
57 var key = localStorage.key(i);
58 if (key.indexOf(STORAGE_NS) == 0) {
59 key = key.substring(STORAGE_NS.length);
60 values.push(key);
61 }
62 }
63 return values;
64 }
65
66 function GM_getResourceURL(resourceName) {
67 throw new Error("not implemented.");
68 }
69
70 function GM_getResourceText(resourceName) {
71 throw new Error("not implemented.");
72 }
73
74 function GM_addStyle(css) { 13 function GM_addStyle(css) {
75 var parent = document.getElementsByTagName("head")[0]; 14 var parent = document.getElementsByTagName("head")[0];
76 if (!parent) { 15 if (!parent) {
77 parent = document.documentElement; 16 parent = document.documentElement;
78 } 17 }
79 var style = document.createElement("style"); 18 var style = document.createElement("style");
80 style.type = "text/css"; 19 style.type = "text/css";
81 var textNode = document.createTextNode(css); 20 var textNode = document.createTextNode(css);
82 style.appendChild(textNode); 21 style.appendChild(textNode);
83 parent.appendChild(style); 22 parent.appendChild(style);
(...skipping 30 matching lines...) Expand all
114 xhr.overrideMimeType(details.overrideMimeType); 53 xhr.overrideMimeType(details.overrideMimeType);
115 } 54 }
116 if (details.headers) { 55 if (details.headers) {
117 for (var header in details.headers) { 56 for (var header in details.headers) {
118 xhr.setRequestHeader(header, details.headers[header]); 57 xhr.setRequestHeader(header, details.headers[header]);
119 } 58 }
120 } 59 }
121 xhr.send(details.data ? details.data : null); 60 xhr.send(details.data ? details.data : null);
122 } 61 }
123 62
124 function GM_registerMenuCommand(commandName, commandFunc, accelKey,
125 accelModifiers, accessKey) {
126 throw new Error("not implemented.");
127 }
128
129 function GM_openInTab(url) { 63 function GM_openInTab(url) {
130 window.open(url, ""); 64 window.open(url, "");
131 } 65 }
132 66
133 function GM_log(message) { 67 function GM_log(message) {
134 window.console.log(message); 68 window.console.log(message);
135 } 69 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698