Script injection of Dart code - dart

On this Dart page https://www.dartlang.org/articles/embedding-in-html/ they say:
“No script injection of Dart code. We do not currently support or recommend dynamically injecting a script tag that loads Dart code. Recent browser security trends, like Content Security Policy, actively prevent this practice.”
What do they mean exactly by “No script injection of Dart code”? Can you show me a concrete example?

I assume this means adding a script tag like
<script src="main.dart" type="application/dart"></script>
imperatively from another Dart script.
Because there is no browser that supports Dart code directly this can't be used in production anyway but I guess it's the same for another <script> tag that loads additional "Dart" code, that was transpiled to JS, dynamically.

Related

Use typescript within <script/> tag

Is it possible to use typescript within <script/> tags?
<script type="text/typescript">
...
</script>
Context:
My platform is ASP.NET Core 2.1 using razor MVC (.cshtml).
Yes it is possible. You can run a filter that replaces all script tags with that type with the compiled source (and the text/javascript type).
I have implemented that before but it was really slow because i did not find any typescript compiler assembly that could be called directly. Calling the tsc executable, which also requires a file (that i temporarily created) takes very long. If you have no static components you could cache it of course.
Also, if you use VS, it does not recognize those tags properly so you do not get highlighting or completion which is a pain.

Distribution of polymer components

We have an application written entirely dart/polymer with quite a few polymer components. We use the custom tags in out index.html and compile to dart with pub build. The compile to javascript creates index.html of 24K lines. Original index.html is 150. The application works perfectly.
However we would like to distribute the code to third party sites so that they also can use the components with custom tags. Ideally by just linking to an already compiled script and simple using our custom tags in their pages.
I know this is possible without polymer. Question is does polymer support this? Is it possible to compile a polymer app and keep to a minimum amount of changes in the html file?
To reduce the problem to an example:
We would like our customers to be able to do some thing like this, without the use of dart sdk:
<head>
<script src="what_ever_required.js"></script>
<script src="our_application.js"></script>
</head>
<body>
<our-custom-tag></our-customer-tag>
<p>What ever else content</p>
</body>
Regards
That's currently not supported.
Currently an application that used Dart code needs to be compiled to JS as a whole at once. There is no way to build parts of a Dart application and compose an application from them later.
With the upcoming DDC (Dart Development Compiler) there might be a way to accomplish that. An experimental approach is https://pub.dartlang.org/packages/polymerize

AngularDart Transformation/Deployments

I am building a client/server app in Dart using Angular for the front-end and Shelf on the backend. When I do a pub build it generates the javascript for the Dart files as expected but is does not replace the dart references in my HTML files. So in my index.html I have the following script reference:
<script type="application/dart" src="main.dart"></script>
This makes my application not load correctly. If I manually change it to
<script src="main.dart.js"></script>
My application works as expected. My question is, is there a way to configure my pub build to do this automatically? Or are dart files references not supposed to be replaced with JS references? If so, how do I build a basic server?
I know this produces an error message in the browser console but never experienced any problems because of this.
I haven't used it myself yet but I think this transformer https://pub.dartlang.org/packages/dart_to_js_script_rewriter does what you want.

How to inject a Dart file in tab?

Dartium does interpret dart files and it opens plenty of fun to develop new toys, and Chrome extensions and apps. But when it comes to do scripts injection in web pages, the executeScript method only takes files supposedly one of the .css and .js formats. Files in other mimetype (typically application/dart) is offbound. Therefore, the question is pretty much naive:
Is there anyway to directly inject a Dart file?
Thanks.
Not a way to directly inject dart-files, but a workaround to inject your dart-application would be to use dart2js and than inject the compiled js-file myDartScript.dart.precompiled.js of your dart-script.
(Use the precompiled-version to avoid errors against the Content Security Policy)
Maybe you also have to inject packages/browser/dart.js and packages/browser/interop.js.
Untested

How to include external Javascript file in a JSF page

I want to include the URL of jQuery UI http://code.jquery.com/ui/1.9.0/jquery-ui.js in my JSF page. I saw many questions which said <h:outputScript> should be used, but in none of the answers I saw an example as to how to include an URL. The answers were only relevant if the js file is present in some folder of the project.
Can someone please help where I have to include it in my page?
Just use plain HTML <script> element the usual way.
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
The <h:outputScript> can only refer local scripts. You'll only miss its dynamic versioning, modularity and relocation advantages, but that should technically not harm for a static and external script.
It's however possible to use a custom ResourceHandler to change the URL of a <h:outputScript> to be an external URL for pure CDN purposes. OmniFaces CDNResourceHandler is such an example.
Unrelated to the concrete problem, PrimeFaces components are built around jQuery/UI. Are you absolutely positive that you need a separate instance of jQuery UI library?

Resources