H
H
habrdima2015-03-27 14:52:59
JavaScript
habrdima, 2015-03-27 14:52:59

Why is the alert repeated?

I can not understand why when you click on the icon again, which calls the code below

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript({
    file: 'sc.js'
  });

 chrome.tabs.query({url: "http://google.ru/*", currentWindow: true}, function(tabs) {
var port = chrome.tabs.connect(tabs[0].id, {name: "knockknock"});
  port.postMessage({q: "ggh"});
})

 });

the alert is repeated, with each new click the alerts are added by one, the alert itself is written in executeScript below
chrome.runtime.onConnect.addListener(function(port) {
     port.onMessage.addListener(function(msg) {
  alert(msg.q)
  })
});

where should be cleaned?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-03-27
@miraage

Try wrapping the calls in a condition:
if (window.top !== window.self)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question