Created
January 8, 2011 18:02
-
-
Save mohamedmansour/771033 to your computer and use it in GitHub Desktop.
Google Chrome Extension to override the Notifications API usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script> | |
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { | |
var notificationObj = request.NotificationCallback; | |
if (notificationObj) { | |
if (notificationObj.url) { | |
// HTML Notification. | |
} | |
else { | |
// TEXT Notification | |
} | |
console.debug(notificationObj); | |
} | |
}); | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a dummy transfer DIV so we can place contents inside. | |
var transferDOM = document.createElement('div'); | |
transferDOM.setAttribute('id', 'transfer-dom-area'); | |
transferDOM.style.display = 'none'; | |
document.body.appendChild(transferDOM); | |
// Addes the function as a script embedded in the DOm. | |
function executeScript(func) { | |
var script = document.createElement('script'); | |
script.appendChild(document.createTextNode('(' + func + ')();')); | |
document.body.appendChild(script); | |
} | |
// Override Notifications | |
executeScript(function() { | |
var exportEvent = document.createEvent('Event'); | |
exportEvent.initEvent('notificationCallback', true, true); | |
window.webkitNotifications.createNotification = function (iconUrl, title, body) { | |
var n = window.webkitNotifications.createNotification(iconUrl, title, body); | |
n.show = function() { | |
var data = JSON.stringify({title: title, body: body, icon: iconUrl}); | |
document.getElementById('transfer-dom-area').innerText = data; | |
window.dispatchEvent(exportEvent); | |
}; | |
return n; | |
} | |
window.webkitNotifications.createHTMLNotification = function (url) { | |
var n = window.webkitNotifications.createHTMLNotification(url); | |
n.show = function() { | |
var data = JSON.stringify({'url' : url}); | |
document.getElementById('transfer-dom-area').innerText = data; | |
window.dispatchEvent(exportEvent); | |
}; | |
return n; | |
}; | |
}); | |
// Listen for that notification callback from your content script. | |
window.addEventListener('notificationCallback', function(e) { | |
var transferObject = JSON.parse(transferDOM.innerText); | |
chrome.extension.sendRequest({NotificationCallback: transferObject}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Notifications Override", | |
"version": "0.1", | |
"description": "Notifications Override", | |
"background_page": "background.html", | |
"permissions": [ | |
"tabs", "http://*/*", "https://*/*" | |
], | |
"content_scripts": [ | |
{ | |
"matches": ["http://*/*"], | |
"js": ["page.js"], | |
"run_at": "document_end", | |
"all_frames": true | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I couldn't get this to work. Is this still hot for you?