سؤال

أنا أعمل على تمديد المتصفح باستخدام crossrider.أنا في حاجة الى ارسال بعض البيانات المنبثقة من extension.js

قانون بلدي المنبثقة

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

مدونة 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
});

قيمة activeTabUrl لا الحصول على تحديث.أنه يعطي قيمة فارغة.P. S :أنا قادرة على التواصل بين background.js و المنبثقة.ولكن لسبب ما appAPI.رسالة.toActiveTab وظيفة لا يعمل بالنسبة لي.حيث أقوم به الخطأ ؟

Background.js (تحرير)

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

تعمل مدونة 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);
        }
    });
    });
  });
هل كانت مفيدة؟

المحلول

في نموذج التعليمات البرمجية هذا ، activeTabUrl متغير سوى مرة واحدة تم تلقي استجابة من extension.js الملف منذ التراسل غير المتزامن حسب التصميم.ومن ثم عند استدعاء alert(activeTabUrl); في رمز الرسالة لم ترد حتى الآن تعود جيئة وذهابا في extension.js رمز وبالتالي قيمة لا تزال فارغة كما كان تهيئة.

استخدام activeTabUrl متغير يجب أن ننتظر mesage من extension.js الملف, وبالتالي يجب وضع التعليمات البرمجية باستخدام متغير في رد من رسالة المستمع ، ويفضل أن يكون ذلك بوصفها وظيفة.لاحظ أيضا أن استخدام تنبيه في منبثقة رمز أسباب المنبثقة لإغلاق وينبغي وبالتالي لا يمكن استخدامها في منبثقة نطاق.

أنا اختبرت التالية المنبثقة البرمجية التي لا بعيدا مع متغير لتجنب الارتباك و يمر علامة التبويب النشطة URL كمعلمة إلى وظيفة دعا في رسالة المستمع ، وأنه يعمل كما هو متوقع:

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
}

[تنويه:أنا Crossrider الموظف]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top