Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: extension/content_script.js

Issue 11092092: Support compiling templates in the browser. Base URL: git@github.com:dart-lang/dart-web-components.git@master
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extension/build.sh ('k') | extension/dart.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Glue code that runs on the page referencing dart web components.
6 // Some logic has to be placed here rather than on the background page
7 // because of chrome security restrictions.
8
9 // Port used to send requests to the background page to proxy urls.
10 var proxyPort = null;
11
12 // Port used to send requests to the background page to parse components.
13 var parsePort = null;
14
15 function endsWith(str, pattern) {
16 return str.lastIndexOf(pattern) == str.length - pattern.length;
17 }
18
19 function onRequestParse() {
20 if (parsePort == null) {
21 parsePort = chrome.extension.connect({name: "parse"});
22 }
23 var l = window.location;
24 var pageUrl = l.protocol + "//" + l.host + l.pathname;
25 parsePort.postMessage({url: pageUrl});
26 parsePort.onMessage.addListener(function(msg) {
27 if (msg.type == "CREATE_DATA_URLS") {
28 var requests = msg.requests;
29 var response = [];
30 for (var i = 0; i < requests.length; i++) {
31 var request = requests[i];
32 var url = request.url;
33 var content = request.content;
34 var contentType =
35 endsWith(url, ".html") ? "text/html" : "application/javascript";
36 var objectUrl = webkitURL.createObjectURL(
37 new Blob([content], {type: contentType}));
38 response.push({url: url, redirectUrl: objectUrl});
39 }
40 if (proxyPort == null) {
41 proxyPort = chrome.extension.connect({name: "proxy"});
42 }
43 proxyPort.postMessage({requests: response});
44 // TODO(jacobr): remove timeout and listen for a message from the
45 // background page instead.
46 var generatedPageUrl = pageUrl.replace(/[/]([^/]+)$/,
47 function(match, p1) { return "/_" + p1 + ".html"});
48 window.setTimeout(function() {
49 for (var i = 0; i < requests.length; i++) {
50 var request = requests[i];
51 var url = request.url;
52 var content = request.content;
53 if (url == generatedPageUrl) {
54 // Replace the contents of the existing document with the
55 // proxied content at the url with an additional .html added.
56 // We prefer this versus redirecting to the page with an extra
57 // .html so that refreshing this page does the right thing.
58 var newDoc = document.open("text/html", "replace");
59 newDoc.write(content);
60 newDoc.close();
61 }
62 }
63 }, 100);
64 }
65 })
66
67 }
68
69 document.addEventListener("DOMContentLoaded", function() {
70 if (document.querySelector("link[rel=components], element, template")) {
71 // TODO(jacobr): should we always parse if there could be components?
72 onRequestParse();
73 }
74 });
75
76 window.addEventListener("message", function(event) {
77 if (event.source != window)
78 return;
79
80 if (event.data.type == "PARSE") {
81 onRequestParse();
82 }
83 }, false);
OLDNEW
« no previous file with comments | « extension/build.sh ('k') | extension/dart.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698