Frage

Ich arbeite an einer Browsererweiterung mit Crossrider.Ich muss einige Daten vom Popup an extension.js senden

Mein Popup-Code

<!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>

Code von Extension.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
});

Der Wert von activeTabUrl wird nicht aktualisiert.Es gibt einen NULL-Wert.P.S.:Ich kann zwischen background.js und Popup kommunizieren.Aber aus irgendeinem Grund funktioniert die Funktion appAPI.message.toActiveTab bei mir nicht.Wo mache ich den Fehler?

Background.js (Bearbeiten)

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);
        }
    });
  });

Arbeitscode von 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);
        }
    });
    });
  });
War es hilfreich?

Lösung

In diesem Codebeispiel ist die activeTabUrl Die Variable wird erst gesetzt, wenn eine Antwort von empfangen wird extension.js Datei, da die Nachrichtenübermittlung von Natur aus asynchron ist.Daher beim Anruf alert(activeTabUrl); Im Code steht, dass die Nachricht noch nicht vom zurückerhalten wurde extension.js Code, daher ist der Wert immer noch null, als er initialisiert wurde.

Um das zu nutzen activeTabUrl Variable müssen Sie auf die Nachricht von warten extension.js Datei, und daher sollten Sie den Code mithilfe der Variablen im Rückruf des Nachrichten-Listeners platzieren, vorzugsweise als Funktion.Beachten Sie außerdem, dass die Verwendung einer Warnung im Popup-Code zum Schließen des Popups führt und daher nicht im Popup-Bereich verwendet werden sollte.

Ich habe den folgenden Popup-Code getestet, der auf die Variable verzichtet, um Verwirrung zu vermeiden, und die URL des aktiven Tabs als Parameter an die im Nachrichten-Listener aufgerufene Funktion übergibt, und es hat wie erwartet funktioniert:

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
}

[Haftungsausschluss:Ich bin ein Crossrider-Mitarbeiter]

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top