我是Chrome扩展程序的新手,似乎无法弄清楚背景概念是如何工作的.我正在构建一个计数器扩展,即使用户关闭扩展(但不是浏览器),并且想要做一个简单的测试,看看我是否可以弄清楚如何使用后台文件.下面是我尝试创建一个功能,每当用户点击选项卡(我的扩展名之外)时激活该功能,当他们点击5个选项卡时,警报就会响起.我无法弄清楚为什么这不起作用.
background.js:
var counter = 0;
chrome.browserAction.onClicked.addListener(function(tab){
counter++;
if (counter == 5) {
alert("Hi");
}
});
Run Code Online (Sandbox Code Playgroud)
manifest.json的:
{
"name": "Hello World!",
"description": "My first packaged app.",
"version": "0.1",
"permissions": ["tabs", "http://*/*"],
"manifest_version":2,
"content_scripts": [ {
"js": [ "jquery-1.9.1.js", "myscript.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_title": "10,000 Hours",
"default_icon": "icon16.png",
"default_popup": "index.html"
},
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
Run Code Online (Sandbox Code Playgroud) 我使用django的内置方法序列化了我的一个对象,然后将其传递给我的模板.当我在html中放置{{goals}}时,数据显示完美.但是,当我尝试通过js脚本访问它时,它不起作用.为什么是这样?我提醒它,它一直未定.
#Python Views
def characterscreen(request):
goals = serializers.serialize("json", Goal.objects.filter(userprofile=userprof))
#goals = Goal.objects.filter(userprofile=userprof).values()
return render_to_response('levelup/characterscreen.html', {'goals': goals}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
class Goal(models.Model):
title = models.CharField(max_length = 30)
userprofile = models.ForeignKey(UserProfile)
type = models.CharField(max_length = 5)
def __unicode__(self):
return str(self.title)
Run Code Online (Sandbox Code Playgroud)
$("body").onload = load_goals();
function load_goals (){
alert(goals);}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% static 'levelup/style.css' %}" />
{% block header %}{% endblock%}
</head>
<body>
<div id="headercontainer">
<div id="header">
</div> …Run Code Online (Sandbox Code Playgroud)