2012-08-12 19 views
5

का उपयोग नहीं किया जा सकता है, मैं पहली बार एड-ऑन विकसित कर रहा हूं। यह स्टेटस बार में एक छोटा विजेट डालता है जो अपठित Google रीडर आइटमों की संख्या प्रदर्शित करता है। इसे समायोजित करने के लिए, ऐड-ऑन प्रक्रिया हर बार Google रीडर एपीआई से पूछताछ करती है और विजेट को प्रतिक्रिया देती है। जब मैं cfx test चलाने मैं इस त्रुटि मिलती है:त्रुटि: पृष्ठ को नष्ट कर दिया गया है और अब

Error: The page has been destroyed and can no longer be used.

मैं विजेट का detach घटना को पकड़ने और जवाब में ताज़ा टाइमर को रोकने के लिए सुनिश्चित किया है, लेकिन मैं अभी भी त्रुटि दिखाई दे रही। मैं क्या गलत कर रहा हूं? यहाँ प्रासंगिक कोड है:

// main.js - Main entry point 
const tabs = require('tabs'); 
const widgets = require('widget'); 
const data = require('self').data; 
const timers = require("timers"); 
const Request = require("request").Request; 

function refreshUnreadCount() { 
    // Put in Google Reader API request 
    Request({ 
     url: "https://www.google.com/reader/api/0/unread-count?output=json", 
     onComplete: function(response) { 
      // Ignore response if we encountered a 404 (e.g. user isn't logged in) 
      // or a different HTTP error. 
      // TODO: Can I make this work when third-party cookies are disabled? 
      if (response.status == 200) { 
       monitorWidget.postMessage(response.json); 
      } else { 
       monitorWidget.postMessage(null); 
      } 
     } 
    }).get(); 
} 

var monitorWidget = widgets.Widget({ 
    // Mandatory widget ID string 
    id: "greader-monitor", 

    // A required string description of the widget used for 
    // accessibility, title bars, and error reporting. 
    label: "GReader Monitor", 
    contentURL: data.url("widget.html"), 
    contentScriptFile: [data.url("jquery-1.7.2.min.js"), data.url("widget.js")], 

    onClick: function() { 
     // Open Google Reader when the widget is clicked. 
     tabs.open("https://www.google.com/reader/view/"); 
    }, 

    onAttach: function(worker) { 
     // If the widget's inner width changes, reflect that in the GUI 
     worker.port.on("widthReported", function(newWidth) { 
      worker.width = newWidth; 
     }); 

     var refreshTimer = timers.setInterval(refreshUnreadCount, 60000); 

     // If the monitor widget is destroyed, make sure the timer gets cancelled. 
     worker.on("detach", function() { 
      timers.clearInterval(refreshTimer); 
     }); 

     refreshUnreadCount(); 
    } 
}); 

// widget.js - Status bar widget script 

// Every so often, we'll receive the updated item feed. It's our job 
// to parse it. 
self.on("message", function(json) { 
    if (json == null) { 
     $("span#counter").attr("class", ""); 
     $("span#counter").text("N/A"); 
    } else { 
     var newTotal = 0; 
     for (var item in json.unreadcounts) { 
      newTotal += json.unreadcounts[item].count; 
     } 

     // Since the cumulative reading list count is a separate part of the 
     // unread count info, we have to divide the total by 2. 
     newTotal /= 2; 
     $("span#counter").text(newTotal); 

     // Update style 
     if (newTotal > 0) 
      $("span#counter").attr("class", "newitems"); 
     else 
      $("span#counter").attr("class", ""); 
    } 

    // Reports the current width of the widget 
    self.port.emit("widthReported", $("div#widget").width()); 
}); 

संपादित: मैं this GitHub repository को अपनी संपूर्णता में परियोजना अपलोड कर दिया है।

+0

यह त्रुटि आपके postMessage लिए आता है। क्या आपने 'monitorWidget.port.emit (" widthReported ", response.json) के साथ प्रयास किया है;'? –

+0

@ चार्ल्स: मैं आपकी टिप्पणी के कारण इस मुद्दे को ठीक करने में सक्षम था। क्या आप इसे एक उत्तर के रूप में पोस्ट कर सकते हैं ताकि आप बक्षीस के लिए योग्य हो? – Pieter

+0

हां मैं इसे अभी करता हूं लेकिन मैं अधिक स्पष्टीकरण नहीं दे सकता:/ –

उत्तर

0

मुझे लगता है कि यदि आप monitorWidget.port.emit("widthReported", response.json); विधि का उपयोग करते हैं तो आप ईवेंट को आग लगा सकते हैं। यह सामग्री स्क्रिप्ट और ऐड-ऑन स्क्रिप्ट के साथ संवाद करने का दूसरा तरीका है।

+0

फिर से धन्यवाद, बक्षीस तुम्हारा है! – Pieter

+0

भविष्य के संदर्भ के लिए, मैं एक पोस्ट जोड़ूंगा [पोस्टमेसेज का उपयोग कर समय के मुद्दे] (https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/content- स्क्रिप्ट /using-postmessage.html) यहां। – Pieter

0

मुझे लगता है कि यह संदेश तब आता है जब आप monitorWidget.postMessage()refreshUnreadCount() पर कॉल करते हैं। इसके लिए स्पष्ट कारण यह होगा: जब आप refreshUnreadCount() पर कॉल करना सुनिश्चित करते हैं, जब कार्यकर्ता अभी भी सक्रिय है, तो यह फ़ंक्शन एक असीमित अनुरोध करेगा जिसमें कुछ समय लग सकता है। तो जब तक यह अनुरोध पूरा हो जाता है तो कार्यकर्ता पहले ही नष्ट हो सकता है।

एक समाधान कार्यकर्ता को refreshUnreadCount() पर पैरामीटर के रूप में पास करना होगा। इसके बाद यह अपना detach श्रोता जोड़ सकता है (अनुरोध पूरा होने पर इसे हटा दें) और प्रतिक्रिया के दौरान कार्यकर्ता को अलग कर दिया गया था, तो प्रतिक्रिया को अनदेखा करें। लेकिन नहीं वास्तव में एक स्वच्छ समाधान -

function refreshUnreadCount(worker) { 
    var detached = false; 
    function onDetach() 
    { 
     detached = true; 
    } 
    worker.on("detach", onDetach); 

    Request({ 
     ... 
     onComplete: function(response) { 
      worker.removeListener("detach", onDetach); 
      if (detached) 
       return; // Nothing to update with out data 

      ... 
     } 
    }).get(); 
} 

फिर, try..catch का उपयोग कर इस स्थिति त्रुटि का पता लगाने और दबाने के लिए शायद आसान हो जाएगा।

+0

यहां अपडेट किया गया main.js: http://pastebin.com/Eiti2Nss मुझे अभी भी 'cfx test' चलाने के बाद त्रुटि मिल रही है।मैंने अभी तक फ़ायरफ़ॉक्स ऐड-ऑन प्रोग्रामिंग में थ्रेड सुरक्षा के बारे में नहीं पढ़ा है, यह मेरी todo सूची पर है। – Pieter

+0

@Pieter: कोई थ्रेड सुरक्षा नहीं है, जावास्क्रिप्ट एकल-थ्रेडेड है - जब तक कि आप एक डोम कार्यकर्ता स्पष्ट रूप से नहीं बनाते (पूरी तरह से एसडीके में आपके पृष्ठ श्रमिकों से संबंधित नहीं)। –

+0

तो मुझे केवल एसिंक्रोनस अनुरोध और टाइमर से सावधान रहना होगा? Http://pastebin.com/ccx80iJV के अनुसार, 'monitorWidget.postMessage (null)' पर एक कॉल अभी भी समस्याएं पैदा कर रहा है। – Pieter

0

मैंने आपके संदेश को आईआरसी पर देखा है, आपके मुद्दों की रिपोर्ट करने के लिए धन्यवाद। आपको एसडीके में कुछ आंतरिक बग का सामना करना पड़ रहा है। मैंने here के बारे में एक बग खोला है।

आपको निश्चित रूप से अपने कोड का पहला संस्करण रखना चाहिए, जहां आप विजेट को संदेश भेजते हैं, यानी widget.postMessage (worker.postMessage के बजाय)। फिर हमें आपके कोड को काम करने के लिए लिंक की गई बग को ठीक करना होगा !!

तब मैं सुझाव देता हूं कि आप सेट इंटरवल को अपरिवर्तित पर ले जाएं, अन्यथा आप प्रति विंडो एक से अधिक अंतराल और अनुरोध करेंगे। यह attach प्रत्येक नई फ़ायरफ़ॉक्स विंडो के लिए ईवेंट निकाल दिया गया है।

+0

अगर मैं रीफ्रेश टाइमर को शीर्ष स्तर पर ले जाता हूं, तो विजेट सफलतापूर्वक संलग्न होने से पहले इसे फायरिंग शुरू कर सकता है? और यदि मैं पहले संस्करण पर वापस लौटता हूं, तो 'रीफ्रेश' न्यूटकाउंट() 'को उस स्थिति में ढूंढें जहां' अनुरोध 'दायर किया गया है, विजेट नष्ट हो गया है और फिर' ऑनम्प्लेट 'हैंडलर' widget.postMessage' को कॉल करने का प्रयास करता है, जो ' अब उपलब्ध नहीं है? मुझे नहीं पता कि थ्रेडिंग एड-ऑन कैसे प्रभावित करता है, इसलिए मुझे नहीं पता कि क्या होगा। अपडेट के बाद 'main.js' [this] (http://pastebin.com/MXEz4hnm) जैसा दिखेगा। – Pieter

संबंधित मुद्दे