P
P
Priney2015-01-28 21:18:21
JavaScript
Priney, 2015-01-28 21:18:21

How to make the Chrome extension react to notifications from the site?

There is a certain site that displays a notification to the browser on some event. It is required that the extension for Google Chrome calls a certain function in case a notification pops up from this site.
The first thought is to change the onshow event in the script responsible for notifications, but as far as I understand, I cannot directly modify site scripts through the extension, I can only interact with the DOM and insert my own scripts.
How to be and what to do? Is there an easier way than copying the entire script and pasting it with the necessary changes through the extension into the site, first deleting the old script from the header?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Bykov, 2015-01-28
@jusio

The first method, the easiest
In the manifest, list the domains from which you can connect:

"externally_connectable": {
  "matches": ["https://somesites.com/*"]
}

In page scripts, it will now be possible to send messages to this extension:
if(chrome && chrome.runtime && chrome.runtime.sendMessage){
var editorExtensionId = "уникальный идентификатор раширения";
chrome.runtime.sendMessage(editorExtensionId, {notification:"Some notification"});
}

Handling messages in the extension:
chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (sender.url != "https://somesites.com")
      return;  
    showNotification(request.notification);
  });

An example from the official documentation
The second method
From the context of the page, you can send messages using window.postMessage. Content script is able to process such messages and then pass them to the extension.
Example from the official documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question