2015-06-05 133 views
1

我需要幫助來解決我的mozilla插件問題。我需要在我的服務器上發送請求以檢查用戶會話的正文中附加我的HTML代碼。請求響應後,我想根據用戶狀態附加HTML。在我的代碼中說jQuery沒有定義。這是我的main.js代碼:需要幫助來修復mozilla插件

var buttons = require('sdk/ui/button/action'); 
var tabs = require("sdk/tabs"); 
var button = buttons.ActionButton({ 
id: "mozilla-link", 
label: "Visit Mozilla", 
icon: { 
    "16": "./icon-16.png", 
    "32": "./icon-32.png", 
    "64": "./icon-64.png" 
} 

}); 
var data = require("sdk/self").data; 
var request = require("sdk/request"); 
var contentScriptString = ''; 
var text =''; 
var pageMod = require("sdk/page-mod").PageMod({ 
include: "*", 
contentScriptFile: data.url("jquery.min.js"), 
contentScriptWhen: "ready", 
onAttach: function(worker) { 
    var tabUrl = worker.tab.url; 

    var mapObj = { 
     'http://': "", 
     'https://': "" 

    }; 
    var re = new RegExp(Object.keys(mapObj).join("|"), "gi"); 
    tabUrl = tabUrl.replace(re, function(matched) { 
     return mapObj[matched]; 
    }); 
    tabUrl = tabUrl.split("/"); 
    if (tabUrl) { 
     request.Request({ 
      url: "https://www.karmora.com/checkstore/" + tabUrl[0], 
      onComplete: function(response) { 
       console.info(response.json); 
       if (response.json.storeId !== null) { 
        var storeName = response.json.storeTitle; 
        var amount = response.json.commPercentage; 
        var store_id = response.json.storeId; 
        text = '<img src="https://www.karmora.com/download.png">'; 
        if (response.json.loginStatus) { 


         if (response.json.visitStatus === true) { 
          text = '<img src="https://www.karmora.com/activated_account.png">'; 
         } else { 
          var url_check = "https://www.karmora.com/index/VisitExtension/" + store_id; 
          request.Request({ 
           url: url_check, 
           onComplete: function(response) { 
            location.href = response.json; 
           } 
          }); 
          console.info(url_check); 
          text = '<a href="https://www.karmora.com/index/VisitExtension/' + store_id + '"><img src="https://www.karmora.com/activate.png"></a>'; 

         } 
        } else { 
         text = '<a href="https://www.karmora.com/"><img src="https://www.karmora.com/download.png"></a>'; 
        } 
        writecanvesPopup(text); 
        //} 
        // }); 

       } 
       function writecanvesPopup(text) { 

        jQuery("body").append('<style>.karmora-login {display: inline-block;float: right;width: 170px;font-size: 14px;height: 34px;margin: 0px 0px 0px 10px;cursor: pointer;}.ebates-notification-close {display: inline-block;float: right;width: 24px;height: 24px;}.ebates-notification-logo {display: inline-block;width: 103px;height: 42px;margin: 8px;}.ebates-notification-container {margin: 0px 10px;line-height: 16px;min-width: 400px;box-sizing: content-box;vertical-align: top;border: 1px solid rgb(170, 170, 170);border-image-source: initial;border-image-slice: initial;border-image-width: initial;border-image-outset: initial;border-image-repeat: initial;border-radius: 5px;padding: 7px 5px 3px;display: inline-block;background: rgb(255, 255, 255);box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 2px;height: 42px;}div.ebates-notification {top: 0px;height: 66px;min-width: 880px;background: rgb(43, 128, 1);float: none;position: fixed;left: 0px;right: 0px;padding: 0px;margin: 0px;overflow: hidden;z-index: 999999999;cursor: default;-webkit-user-select: none;font-size: 14px;line-height: 16px;display: inline-block;box-sizing: content-box;}</style><div class="ebates-notification" id="notification" style="height: 66px; display: inline-block; top: 0px;"><div style="padding: 5px; float: none; text-align: left;"><div class="ebates-notification-close" id=""><a href="#" onclick="document.getElementById(\'notification\').style.display=\'none\'"><img src="https://staging.karmora.com/public/images/close.png"></a></div><div class="ebates-notification-logo"><img src="https://www.karmora.com/logo48.png"></div><span class="ebates-notification-container"><div class="karmora-login" title="">' + text + '</div><div class="ebates-notification-link">Get <span class="ebates-notification-cashback">' + amount + '</span></div><div class="ebates-notification-store">at ' + storeName + '</div></span></div></div>'); 
       } 

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

回答

1

這是因爲您從瀏覽器上下文中調用jQuery,而不是在DOM上下文中調用。您不能在瀏覽器上下文中操作頁面。看看可以操縱DOM的Content Scripts

看到它在行動:https://gist.github.com/AdrianArroyoCalle/f8535807ebd738e938b7

+0

#Adrian我這樣做之前,無需用戶會話。但現在我想在追加html之前檢查用戶會話。我使用請求方法進行調用,現在可以找出添加我的html的方式。內容腳本不起作用。 –

+0

您可以使用事件在瀏覽器腳本和內容腳本之間進行內部通信。這是不可能的,沒有內容腳本操縱dom –

+0

給我你的Skype或電子郵件。所以我可以詳細解釋你的整個過程。 –