Domanda

Sto lavorando su un'estensione del browser usando Crossrider.Ho bisogno di inviare alcuni dati dal popup a estensione.js

Il mio codice di popup

<!DOCTYPE html>
<html>
<head>
<!-- This meta tag is relevant only for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<script type="text/javascript">
/************************************************************************************
  This is your Popup Code. The crossriderMain() code block will be run
  every time the popup is opened.

  For more information, see:
  http://docs.crossrider.com/#!/api/appAPI.browserAction-method-setPopup
*************************************************************************************/

function crossriderMain($) {
  // var to store active tab's URL
  var activeTabUrl = null;

  // Message listener for response from active tab
  appAPI.message.addListener(function(msg) {
    if (msg.type === 'active-tab-url') activeTabUrl = msg.url;
  });

  // Request URL from active tab
  appAPI.message.toActiveTab({type: 'active-tab-url'});

    alert(activeTabUrl);
  // THE REST OF YOUR CODE
}
</script>

</head>
<body>

Hello World

</body>
</html>
.

Codice di estensione.js

appAPI.ready(function($) {
  // Message listener
  appAPI.message.addListener(function(msg) {
    if (msg.type === 'active-tab-url')
      // Send active tab's URL to popup
      appAPI.message.toPopup({
        type: 'active-tab-url',
        url:encodeURIComponent(location.href)
      });
  });

  // THE REST OF YOUR CODE
});
.

Il valore di ActiveTaburl non è aggiornato.Dà valore nullo. P.S: Sono in grado di comunicare tra background.js e popup.Ma per qualche motivo Appoti.Message.TOACTIVETAB La funzione non funziona per me.Dove sto facendo l'errore?

Background.js (modifica)

var tabUrl='';
 /* appAPI.tabs.getActive(function(tabInfo) {
        tabUrl = tabInfo.tabUrl;
        }); */
 appAPI.message.addListener(function(msg) {
        appAPI.tabs.getActive(function(tabInfo) {
        tabUrl = tabInfo.tabUrl;
        });
       var dataString = '{"url":"'+tabUrl+'","access":"'+msg.access+'","toread":"'+msg.toread+'","comment":"'+msg.comment+'"}';
     alert(dataString);
     appAPI.request.post({
        url: 'REST API URL',
        postData: dataString,
        onSuccess: function(response, additionalInfo) {
            var details = {};
            details.response = response;
            appAPI.message.toPopup({
            response:response
        });

        },
        onFailure: function(httpCode) {
        //  alert('POST:: Request failed. HTTP Code: ' + httpCode);
        }
    });
  });
.

Codice di lavoro di background.js

appAPI.message.addListener(function(msg) {
    appAPI.tabs.getActive(function(tabInfo) {     
       var dataString = '{"url":"'+tabInfo.tabUrl+'","access":"'+msg.access+'","toread":"'+msg.toread+'","comment":"'+msg.comment+'"}';
    // alert(dataString);
     appAPI.request.post({
        url: 'http://fostergem.com/api/bookmark',
        postData: dataString,
        onSuccess: function(response, additionalInfo) {
            var details = {};
            details.response = response;
            appAPI.message.toPopup({
            response:response
        });

        },
        onFailure: function(httpCode) {
        //  alert('POST:: Request failed. HTTP Code: ' + httpCode);
        }
    });
    });
  });
.

È stato utile?

Soluzione

In questo campione di codice, la variabile ActiveTaburl viene impostata solo una volta ricevuta una risposta dal file extension.js poiché la messaggistica è asincrona dal design. Quindi, quando si chiama alert(activeTabUrl); nel codice, il messaggio non è stato ancora ricevuto indietro del codice estensione.js , quindi il valore è ancora nullo come è stato inizializzato.

Per utilizzare la variabile ActiveTaburl è necessario attendere il file di mesage dal file extensione.js , e quindi è necessario posizionare il codice usando la variabile nel callback del Ascoltatore dei messaggi, preferibilmente come funzione. Si noti inoltre che l'utilizzo di un avviso nel codice popup, i popup si chiudono e dovrebbero quindi essere utilizzati nell'ambito del popup.

Ho testato il seguente codice popup, che è lontano con la variabile per evitare confusione e supera l'URL della scheda attiva come un parametro sulla funzione chiamato nel listener del messaggio e ha funzionato come previsto:

function crossriderMain($) {
  // Message listener for response from active tab
  appAPI.message.addListener(function(msg) {
    if (msg.type === 'active-tab-url') ShowPageUrl(msg.url);
  });

  function ShowPageUrl(url) {
    $('#page-url').html('<b>Page URL</b>: ' + url);
  }

  // Request URL from active tab
  appAPI.message.toActiveTab({type: 'active-tab-url'});

    //alert(activeTabUrl);
  // THE REST OF YOUR CODE
}
.

[ Disclaimer : Io sono un dipendente Crossrider]

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top