Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created January 8, 2011 18:02
Show Gist options
  • Save mohamedmansour/771033 to your computer and use it in GitHub Desktop.
Save mohamedmansour/771033 to your computer and use it in GitHub Desktop.
Google Chrome Extension to override the Notifications API usage.
<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>
// 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});
});
{
"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
}
]
}
@0xmtn
Copy link

0xmtn commented Mar 27, 2018

Hello,
I couldn't get this to work. Is this still hot for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment