Polymer does not work in Chrome Extensions
Polymer does not work in Chrome Extensions without modifications. The reason is restrictions by the Chrome Extensions infrastructure preventing inline scripts from running.
To allow Polymer to function in Chrome Extensions without using tools that will slow development (tools like Vulcanize), and to help with its adoption, please consider having the Polymer components use external scripts only. I believe that some components already do this.
I'm curious how much vulcanize slows you down? Maybe we can do something to speed that up.
Refactoring all of our components to be CSP friendly would be something we would need to weigh very heavily. I'd be uncomfortable losing the organizational and readability benefits of the current style.
One thought that comes to mind: what if there were a tool that runs vulcanize individually on each component you depend on? Pre-vulcanization. This could be done after bower updates.
Alternatively for now, you might want to split up your code into a global import file that's vulcanized ahead of time, freeing you to write your extension's code in a CSP-friendly manner. On Fri, Sep 19, 2014 at 1:17 PM viridium [email protected] wrote:
Polymer does not work in Chrome Extensions without modifications. The reason is restrictions by the Chrome Extensions infrastructure preventing inline scripts from running.
To allow Polymer to function in Chrome Extensions without using tools that will slow development (tools like Vulcanize), and to help with its adoption, please consider having the Polymer components use external scripts only. I believe that some components already do this.
— Reply to this email directly or view it on GitHub https://github.com/Polymer/polymer/issues/762.
I have no problem with manually externalizing each component's script as a stop gap measure.
I also don't want to bundle components together. It makes it harder for me to look into a component's source code, and to debug.
In addition, I would have to rerun vulcanization each time I change an import in my files. Unless I want to pay the price of using a single monolith, in which case I would still have to run it each time I update my component repository.
Bundling also has the problem of obscuring the dependencies of my components, because I would have to replace my imports with a single import.
My own components may complicate matters further, as they would be under heavy editing throughout the development process.
I like the idea of a postprocessing step during 'bower install'. It enables the developer to keep components separate while making them CSP aware. Though this CSP awareness step seems Polymer specific and I don't know if Bower manipulating Polymer is consistent with Bower's philosophy.
That said, I would prefer one time configuration of Bower, maybe on a per repository basis, rather than constant command line switch reminders. This would amount to one time maintenance, which is not as good as zero maintenance, but it is much better than constant maintenance. This should be complemented with an FAQ entry to give guidance to the poor souls that forget to use this feature.
Alternatively, Polymer could be pre-built in two modes: the current mode, and a CSP-friendly mode. Does Bower support download channels? which could be used to control the mode that the developer wants to use.
Or, the two modes could be bundled in each component, with, for instance, a paper-button.html, and a paper-button-csp.html.
Yeah, of these options, I think a postprocessing step in bower makes the most sense. We're going to be taking a look at how we're handling bower soon - seems reasonable to fold this into that work
+1 - I wanted to casually use polymer in an extension (and have no experience with grunt/vulcanize), so I downloaded the files and tried to get it to launch, and smacked into this. It would be nice to have some workaround that didn't involve new tech for a simple javascript extension - perhaps providing the monolithic js file or something similar.
FYI, postprocessing is already using in chromedevtool, They called that 'Refactor for CSP`. I was inspired that, I am making gulp plugin to postprocessing. but almost logic is already developed in vulcunize. So I prefer using vulcunize to using other tools for CSP
@salamanders: yes, this flies in the face of extension dev, since the familiar approach to writing Chrome extensions is to just grab libraries from wherever, put them together under a single directory, and start talking to them.
@nevir, @ragingwind : Vulcanize has a show stopping problem.
I've just written a script on top of Vulcanize, script which takes all my component HTML files and runs Vulcanize with the CSP option on them. It works well on my test data (polymer, core-component-page) for the first go around. The second time I run it, it empties out all the external JavaScript files that it created the first time.
Idempotency aside (I could resolve this idempotency issue by keeping track of which files have already been vulcanized -- with a file fingerprint, probably), this means that I now have to navigate around any components that already have their scripts externalized, because their author may have chosen to use external scripts. Good luck with that, right? And, for any component with both inline and external scripts, it would seem that the answer is even worse: if I run Vulcanize on the component, it will erase the component's external script. If I don't run it, the component won't work in a Chrome extension. In other words, the component won't work in a Chrome extension whether or not I run Vulcanize on it.
@viridium It would be nice to see your script. I am considering using polymer in an extension and find that even the CDE's 'refactor for CSP' doesn't work - it's nice in that it's a one-shot operation so I can just point it at bower_components, but using those components in my extension actually still produces errors. I did a quick test here :
https://github.com/davidmaxwaterman/polymer-in-event-page
It's just an background (event) page (ie html) that uses a single custom (manually CSPed) polymer element that itself uses core-ajax. Ref: https://github.com/dart-lang/chromedeveditor/issues/3584
I will publish the script once I have it worked out. I was talking about a showstopper two comments ago, but I think that may be a red herring. It may be due to how the script currently works:
- Copy my_file.html to tmp.html.
- Vulcanize from tmp.html to my_file.html + my_file.js.
If I do this a second time around, I will be vulcanizing a tmp.html that already points to my_file.js. So OK, that causes Vulcanize to get confused, because an input file is also an output file. It should know better and prefetch its inputs before writing to its outputs, but oh well. Maybe we can get past this weakness by doing instead:
- Vulcanize my_file.html to tmp.html + tmp.js.
- Vulcanize tmp.html to my_file.html + my_file.js. That should give Vulcanize inputs that are not also outputs.
Will adjust my script and get back to you.
I called this cspize.
Update 2: with an optimization that skips files for which the .js file already exists. This nicely skips my custom components, plus it speeds the whole thing up for subsequent runs.
Update 3: goes at most two levels deep. Makes is less friendly to executions with specific component directories as arguments.
#!/bin/bash
# cspize v1.0
# CSPizes all the HTML files in each of the directories passed
# as command line arguments. If no arguments are supplied,
# the script assumes bower_components.
# The script will re-process previously processed files, the
# result being a no-op.
# For files without scripts, empty script files will be created.
# This behavior is specific to Vulcanize.
do_cspize() {
local file=$1
# This added in update 2.
local js_file=${file%.*}.js
if [ -f "$js_file" ]; then
return 0
fi
local temp=$file.cspize-temp
# Vulcanize source -> temp.
if ! vulcanize --inline "$file" --output "$temp"; then
rm -f "$temp"
echo Error at "$file". >&2
return 1
fi
# Vulcanize temp -> source.
if ! vulcanize --csp "$temp" --output "$file"; then
rm -f "$temp"
echo Error at "$file". File may be corrupt. >&2
return 1
fi
rm -f "$temp"
echo "$file"
return 0
}
main() {
if [ $# == 0 ]; then
do_cspize bower_components
return $?
fi
for dir in "$@"; do
# -depth 2 added in update 3.
find "$dir" -depth 2 -type f -name \*.html -o -name \*.htm | while read file; do
do_cspize "$file" || return 1
done
done
return 0
}
main "$@"
exit $?
When I try this on my test code:
https://github.com/davidmaxwaterman/polymer-in-event-page
on the bower_components/ directory, after 'bower install', of course, I get some errors. Yes, I didn't see that you called it cspize and called it instead viridium.sh. These are the errors :
$ ./viridium.sh bower_components/
bower_components/polymer/polymer.html
bower_components/polymer/layout.html
bower_components/core-component-page/core-component-page.html
bower_components/core-component-page/demo.html
bower_components/core-component-page/index.html
fs.js:432
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory '/home/davidmaxwaterman/z/cde_projects/polymer-in-event-page/bower_components/polymer-test-tools/chai/chai.js'
at Object.fs.openSync (fs.js:432:18)
at Object.fs.readFileSync (fs.js:289:15)
at readFile (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/vulcan.js:54:20)
at Object.<anonymous> (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/vulcan.js:84:21)
at exports.each (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/node_modules/cheerio/lib/api/traversing.js:268:24)
at inlineScripts (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/vulcan.js:79:23)
at Object.handleMainDocument [as processDocument] (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/vulcan.js:178:5)
at /home/davidmaxwaterman/npm/lib/node_modules/vulcanize/bin/vulcanize:74:10
at /home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/vulcan.js:31:5
at Object.processOptions (/home/davidmaxwaterman/npm/lib/node_modules/vulcanize/lib/optparser.js:78:3)
Error at bower_components/core-ajax/tests/html/core-ajax-response-and-error.html.
[polymer-in-event-page]$ git config -l
user.name=Max Waterman
[email protected]
diff.tool=gvimdiff
color.ui=auto
push.default=simple
core.autocrlf=false
core.filemode=false
branch.autosetuprebase=always
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[email protected]:davidmaxwaterman/polymer-in-event-page.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.master.rebase=true
I think I just saw an issue you opened about something like this - yeah, #819. Anyway, just sharing my results.
Thanks!
A few things could be done to alleviate such problems:
- We could let the script run even if it encounters an error, and fix any errors manually.
- We could skip files like index.html, or demo.html. This is trickier, because nothing prevents components from using those file names as part of the actual component.
I recommend going with 1. This means removing the || return 1 next to the call to do_cspize. Also make sure you've applied my updates 2 and 3 to optimize the script and to reduce its scope.
I think I understand what @ragingwind means in #819. Vulcanize brings the transitive dependency scripts of our component into the output script, which is not desirable for my purpose. So the script that I offered before is wrong.
@ragingwind said (in #819): I think, you want to separate polymer file into js and html. If so vulcanize is will not work properly as you want. vulcanize is tool that make a set of component file, even it has a CSP option. IMHO, We need another tool to make separated js and html files for CSP. I tried to make (It just a experimental) that https://github.com/ragingwind/gulp-cspify/blob/master/index.js
@ragingwind: could you explain a bit how one might use your experimental tool in a shell script?
@viridium Sorry. you can't use it. I just posted to show a sample for understanding. i've discontinued the project for a while. I'm testing new approach now.
I will then look for alternative technologies to build my Chrome Extension.
It's a pity, because we're talking about a broken use case for the benefit of "organizational and readability benefits", benefits which one might argue are subjective, since the separation of CSS and JavaScript into separate files is an equally beneficial and prolific approach.
At the end of the day, we can't use Polymer in Chrome extensions because of aesthetic concerns. It's frustrating, to say the least.
Indeed, I'm avoiding using Polymer too in that part of my app - our team prefer to have no build step than to use polymer. It's a shame since there's little reason (that I can see) why we can't have both inline and csp versions, with one auto-generated from the other.
FYI, we've been attacking this problem at the root. Static inline-scripts in imports pose no actual security danger, so there is work underway at the Browser level to modify the unsafe-inline sweep not to balk at these scripts. Once that is done, all of this goes away.
For folks that are interested, the Chromium bug is here: https://code.google.com/p/chromium/issues/detail?id=393307
thanks for information. @sjmiles, @ebidel Actually I was developing a new workflow for chromeapps with Polymer. but I couldn't find that enough. I've met some issues while using vulcanize. so I've made patch or another tools to avoid or workaround the problems. IMO best case is no build, no vulcanize in developing Chrome Apps. so should I wait until that the issue of browser(unharmful inline-script) gets solved? or Do you have any plan for developing chrome apps? when do you expect to solve the issue?