在Chrome扩展中使用JQuery的post函数 [英] Using JQuery's post function in chrome extension

查看:413
本文介绍了在Chrome扩展中使用JQuery的post函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的档案:

清单:

  {
name:Send to server ,

version:1.1,

background:{page:background.html},

permissions:[experimental,tabs,< all_urls>],

browser_action:{name:Send to server,default_icon:icon bng,default_popup:popup.html},

图标:{16:16.png},

manifest_version :2

}

background.html非常简短,只是指向http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js

background.js。
(我用它只是为了能够包含JQuery并在background.js中使用它,还有其他方法吗?)

background.js:

  chrome.tabs.onUpdated.addListener(SendToServer); 

函数SendToServer(tabId,changeInfo,tab){
chrome.tabs.getSelected(null,function(tab){

if(changeInfo.status == ='complete'){
chrome.experimental.infobars.show({
tabId:tab.id,
path:infobar.html
});

var strVar =页面已加载;

alert(发送之前:+ strVar);

$ .post(http:/ / localhost / MyDomain,{var:strVar},
function(strVar){alert(Sending ...);}
);
}
})
};

现在,alert(发送之前:+ strVar);被执行,所以在这里我做得很好。但是.post没有完成。



任何想法?



谢谢,

解决方案

如果您调试您的后台页面,您将发现存在安全错误。您正试图从远程源(ajax.googleapis.com)加载JavaScript,而您尚未在清单文件中明确允许该JavaScript。如果没有jQuery, $。post 无法运行。您需要放松默认内容安全策略(请务必加载



如果事实如此,您应该在这种特殊情况下实际执行,即将jQuery文件包含在您的扩展和加载中它相应地在 backtround.html 中。扩展程序不是网站,与简单硬盘提取相比,远程调用花费了大量时间。另外,如果没有互联网连接,使用远程JavaScript文件会使分机无法使用/不稳定。


I'm trying to post to localhost server from within a chrome extension and it doesn't seem to work.

Here are my files:

manifest:

{
"name": "Send to server",

"version": "1.1",

"background": { "page": "background.html"},

"permissions": ["experimental", "tabs", "<all_urls>"],

"browser_action": {"name": "Send to server", "default_icon": "icon.png", "default_popup":"popup.html" },

"icons": { "16": "16.png" },

"manifest_version": 2

}

The background.html is very brief, it just points to "http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" and to "background.js". (I use it just to be able to include JQuery and use it at background.js. Is there another way for it?)

background.js:

chrome.tabs.onUpdated.addListener(SendToServer);

function SendToServer(tabId, changeInfo, tab) {
chrome.tabs.getSelected(null, function(tab) { 

    if (changeInfo.status === 'complete'){
        chrome.experimental.infobars.show({
                tabId: tab.id,
                path: "infobar.html"
            });

            var strVar = "A page was loaded";

            alert("Before sending: " + strVar);

            $.post("http://localhost/MyDomain", {var: strVar}, 
                function(strVar ) { alert("Sending..."); }
            );
        }
  })
};

Now, alert("Before sending: " + strVar); is performed, so till here I'm doing well. But the .post isn't done.

Any idea?

Thanks,

解决方案

If you debug your background page you will find out that there is a security error. You are trying to load javascript from remote source (ajax.googleapis.com) while you haven't explicitly allowed that in the manifest file. And without jQuery, $.post can't run. You need to relax default Content Security Policy (be sure to load jQuery over https).

If fact, what you should actually do in this particular case, is to include jQuery file in your extension and load it accordingly in backtround.html. Extension is not a website, remote call takes a lot of time compared to simple HDD fetch. In addition, using remote javascript files makes extension unusable/unstable when there is no internet connection.

这篇关于在Chrome扩展中使用JQuery的post函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆