Move r.config into an r.setup() call after modules load.

The inline script now has id="config" and is in JSONP format, which will
make it easier for browser extensions and greasemonkey scripts to access
reddit config variables in isolated environments.
This commit is contained in:
Max Goodman
2011-11-08 12:43:02 -08:00
parent 3ef79bf5ee
commit 777efab2df
6 changed files with 52 additions and 57 deletions

View File

@@ -1,5 +1,11 @@
r = window.r || {}
r.setup = function(config) {
r.config = config
// Set the legacy config global
reddit = config
}
$(function() {
r.login.ui.init()
r.analytics.init()

View File

@@ -24,7 +24,7 @@
from r2.lib.template_helpers import static
from r2.lib import js
%>
<%namespace file="utils.html" import="js_preamble, googleanalytics"/>
<%namespace file="utils.html" import="js_setup, googleanalytics"/>
<html xmlns="http://www.w3.org/1999/xhtml" lang="${c.lang}">
<head>
<link rel="apple-touch-icon"
@@ -43,10 +43,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
${self.robots()}
<link rel="stylesheet" href="${static('compact.css')}" type="text/css" media="screen" />
<script type="text/javascript">
${js_preamble()}
</script>
${unsafe(js.use('jquery', 'mobile'))}
${js_setup()}
${googleanalytics('mobile')}
</head>
${self.bodyHTML()}

View File

@@ -26,7 +26,7 @@
from r2.models import Link, Comment, Subreddit
from r2.lib.tracking import UserInfo
%>
<%namespace file="utils.html" import="js_preamble, googleanalytics"/>
<%namespace file="utils.html" import="js_setup, googleanalytics"/>
<html xmlns="http://www.w3.org/1999/xhtml" lang="${c.lang}"
xml:lang="${c.lang}" ${c.lang_rtl and unsafe('dir="rtl"') or ''}>
<head>
@@ -39,15 +39,9 @@
<meta name="viewport" content="width=800, initial-scale=1" />
${self.robots()}
##these are globals, so they should be run first
##things that need are referenced by the reddit, the buttons, and
##the frame should go here
<script type="text/javascript">
${js_preamble()}
</script>
${self.stylesheet()}
${self.javascript()}
${js_setup()}
##things here may depend on globals, or included js, so we run them last
<script type="text/javascript">

View File

@@ -25,15 +25,13 @@
from r2.lib.utils import query_string
from r2.lib.filters import _force_unicode
%>
<%namespace file="utils.html" import="js_preamble"/>
<%namespace file="utils.html" import="js_setup"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>${thing.title}</title>
${unsafe(js.use('jquery', 'mobile'))}
<script type="text/javascript">
${js_preamble()}
</script>
${js_setup()}
<link rel="stylesheet" href="${static('compact.css')}"
type="text/css" media="screen" />
</head>

View File

@@ -1,14 +1,12 @@
<%namespace file="reddit.html" import="javascript"/>
<%namespace file="utils.html" import="js_preamble"/>
<%namespace file="utils.html" import="js_setup"/>
<html>
<head>
<title>escappit</title>
<% from r2.lib import js %>
<script type="text/javascript">
${js_preamble()}
</script>
${unsafe(js.use('jquery', 'reddit'))}
${js_setup()}
</head>
<body>
<script type="text/javascript">

View File

@@ -374,46 +374,47 @@ ${unsafe(txt)}
</%def>
<%def name="js_preamble()">
<%
<%def name="js_setup()">
<%
from r2.lib.template_helpers import get_domain
use_https_endpoint = request.host == g.domain or request.host.endswith("." + g.domain)
%>
r = {};
r.config = reddit = {
/* is the user logged in */
logged: ${c.user_is_loggedin and ("'%s'" % c.user.name) or "false"},
/* the subreddit's name (for posts) */
post_site: "${c.site.name if not c.default_sr else ''}",
/* are we in an iframe */
cnameframe: ${"true" if (c.cname and not c.authorized_cname) else "false"},
/* this page's referer */
referer: "${request.referer or ""}",
/* the user's voting hash */
modhash: ${"'%s'" % c.modhash or "false"},
/* the current rendering style */
renderstyle: "${c.render_style}",
/* current domain */
cur_domain: "${get_domain(cname = c.frameless_cname, subreddit = False,
no_www = True)}",
/* where do ajax request go? */
ajax_domain: "${get_domain(cname=c.authorized_cname, subreddit = False)}",
extension: '${c.extension}',
https_endpoint: '${g.https_endpoint if use_https_endpoint else ""}',
/* debugging? */
debug: ${"true" if g.debug else "false"},
vl: {},
sr: {},
status_msg: {
fetching: "${_('fetching title...')}",
submitting: "${_('submitting...')}",
loading: "${_('loading...')}"
},
is_fake: ${'true' if isinstance(c.site, FakeSubreddit) else 'false'},
tracking_domain: '${g.tracking_domain}',
adtracker_url: '${g.adtracker_url}',
clicktracker_url: '${g.clicktracker_url}'
};
<script type="text/javascript" id="config">
r.setup({
/* is the user logged in */
logged: ${c.user_is_loggedin and ("'%s'" % c.user.name) or "false"},
/* the subreddit's name (for posts) */
post_site: "${c.site.name if not c.default_sr else ''}",
/* are we in an iframe */
cnameframe: ${"true" if (c.cname and not c.authorized_cname) else "false"},
/* this page's referer */
referer: "${request.referer or ""}",
/* the user's voting hash */
modhash: ${"'%s'" % c.modhash or "false"},
/* the current rendering style */
renderstyle: "${c.render_style}",
/* current domain */
cur_domain: "${get_domain(cname = c.frameless_cname, subreddit = False,
no_www = True)}",
/* where do ajax request go? */
ajax_domain: "${get_domain(cname=c.authorized_cname, subreddit = False)}",
extension: '${c.extension}',
https_endpoint: '${g.https_endpoint if use_https_endpoint else ""}',
/* debugging? */
debug: ${"true" if g.debug else "false"},
vl: {},
sr: {},
status_msg: {
fetching: "${_('fetching title...')}",
submitting: "${_('submitting...')}",
loading: "${_('loading...')}"
},
is_fake: ${'true' if isinstance(c.site, FakeSubreddit) else 'false'},
tracking_domain: '${g.tracking_domain}',
adtracker_url: '${g.adtracker_url}',
clicktracker_url: '${g.clicktracker_url}'
})
</script>
</%def>
<%def name="googleanalytics(uitype)">