Complete reference for all Greasemonkey APIs supported by CodeTweak with practical examples and best practices.
Stores a value that persists across page loads and browser sessions.
Parameter | Type | Description |
---|---|---|
key | string | Unique identifier for the stored value |
value | any | Value to store (string, number, boolean, object) |
// Store different types of data
GM_setValue('username', 'john_doe');
GM_setValue('score', 1250);
GM_setValue('settings', {theme: 'dark', notifications: true});
GM_setValue('lastVisit', new Date().toISOString());
Retrieves a previously stored value, with optional default fallback.
Parameter | Type | Description |
---|---|---|
key | string | Key of the value to retrieve |
defaultValue | any | Value returned if key doesn't exist |
// Get stored values with defaults
const username = GM_getValue('username', 'anonymous');
const score = GM_getValue('score', 0);
const settings = GM_getValue('settings', {theme: 'light'});
// Use the retrieved values
console.log(`Welcome back, ${username}! Your score: ${score}`);
Removes a stored value permanently.
// Clear user data
GM_deleteValue('username');
GM_deleteValue('score');
// Clear all data on reset
function resetUserData() {
GM_deleteValue('settings');
GM_deleteValue('resetRequested');
}
Returns an array of all stored keys for this script.
// List all stored keys
const keys = GM_listValues();
console.log('Stored keys:', keys);
// Export all data
const exportData = {};
keys.forEach(key => {
exportData[key] = GM_getValue(key);
});
console.log('Export data:', exportData);
Opens a URL in a new browser tab with optional configuration.
Parameter | Type | Description |
---|---|---|
url | string | URL to open |
options | object | Configuration object (optional) |
options.active | boolean | Whether to focus the new tab |
// Open in background tab
GM_openInTab('https://example.com', {active: false});
// Open in focused tab
GM_openInTab('https://github.com', {active: true});
// Simple usage
GM_openInTab('https://google.com');
Displays a desktop notification to the user.
Parameter | Type | Description |
---|---|---|
options.text | string | Notification message |
options.title | string | Notification title (optional) |
options.image | string | Icon URL (optional) |
options.onclick | function | Click handler (optional) |
// Simple notification
GM_notification({
text: 'Script executed successfully!',
title: 'CodeTweak'
});
// Advanced notification with callback
GM_notification({
text: 'New message received',
title: 'Chat App',
image: 'https://example.com/icon.png',
onclick: function() {
window.focus();
console.log('Notification clicked');
}
});
Injects CSS styles into the current page.
// Add custom styles
GM_addStyle(`
.my-custom-class {
background: #f0f0f0;
padding: 10px;
border-radius: 5px;
}
.highlight {
background: yellow;
font-weight: bold;
}
`);
// Apply styles to existing elements
document.querySelectorAll('.important').forEach(el => {
el.classList.add('highlight');
});
Creates and appends a new DOM element with specified attributes.
Parameter | Type | Description |
---|---|---|
parent | Element | Parent element to append to |
tag | string | HTML tag name |
attributes | object | Element attributes (optional) |
// Add a button to the page
const button = GM_addElement(document.body, 'button', {
textContent: 'Click Me!',
onclick: () => alert('Button clicked!')
});
// Add a styled container
const container = GM_addElement(document.body, 'div', {
id: 'my-container',
style: 'position: fixed; top: 10px; right: 10px; z-index: 9999;'
});
Makes cross-origin HTTP requests that bypass CORS restrictions.
Parameter | Type | Description |
---|---|---|
options.method | string | HTTP method (GET, POST, etc.) |
options.url | string | Request URL |
options.headers | object | Request headers (optional) |
options.data | string | Request body (optional) |
options.onload | function | Success callback |
options.onerror | function | Error callback |
// GET request
GM_xmlhttpRequest({
method: 'GET',
url: 'https://api.example.com/data',
headers: {
'User-Agent': 'CodeTweak Script',
'Accept': 'application/json'
},
onload: function(response) {
console.log('Status:', response.status);
console.log('Response:', response.responseText);
}
});
// POST request with data
GM_xmlhttpRequest({
method: 'POST',
url: 'https://api.example.com/submit',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify({message: 'Hello World'}),
onload: function(response) {
console.log('Response:', response.responseText);
}
});
Retrieves the content of a bundled resource as text.
// Get CSS resource and inject it
const customCSS = GM_getResourceText('myStyles');
GM_addStyle(customCSS);
// Get JSON config file
const configText = GM_getResourceText('config');
const config = JSON.parse(configText);
console.log('Config loaded:', config);
Copies text to the system clipboard.
Parameter | Type | Description |
---|---|---|
text | string | Text to copy to clipboard |
type | string | MIME type (optional, defaults to 'text/plain') |
// Copy simple text
GM_setClipboard('Hello, World!');
// Copy formatted data
const data = {name: 'John', age: 30};
GM_setClipboard(JSON.stringify(data, null, 2));
// Copy with specific MIME type
GM_setClipboard('Hello
', 'text/html');