0

I am currently playing around with embedded browsers like CefSharp. I am injecting javascript into these browsers and thus have some large text strings.

If you are not following me I mean like this

str = "(function dosomething() " _
  + "{ " _
    + "var title = 'Hi'; " _
     + "return title; " _
  + "})()"

With much larger string/functions making them, reading them and editing them is not easy.

I have created a .js file in visual studio which of course is great with intellisense etc.

So I am wondering if I could somehow use the functions I write in the .js file by turning them into a string or turn them into resrouces or something. I really dont know the correct terminology and thus what I should search with Google for.

CefSharp added as a tag as people using it may have another solution to this issues.

I dont want to read the text from a whole file, just a part of it.

darbid
  • 2,545
  • 23
  • 55
  • 1
    Possible duplicate of [Read files from a Folder present in project](https://stackoverflow.com/questions/13762338/read-files-from-a-folder-present-in-project) – GSerg Jan 10 '19 at 14:36
  • VB hasn't required line continuation characters for a long time and VB 2015 and later support multiline `String` literals so there's really very little you actually need to do. Just write `str = ""` in VB and then copy your JavaScript function and paste it between the double quotes. That's it, that's all. – jmcilhinney Jan 10 '19 at 15:00
  • @GSerg not even close. If i wanted to create 100 .js files each with 1 function and then read in the whole text of the file then you might be getting warm. – darbid Jan 10 '19 at 15:01
  • @jmcilhinney I did not know that but it does yes. – darbid Jan 10 '19 at 15:05
  • For the record the duplicate that GSerg suggested isn't about reading hundreds of files, it's about reading one file located in the same directory as your application (.exe). The `File.ReadAllLines()` call can be substituted with `File.ReadAllText()` to read the entire file into a _single string_, instead of reading it into an array where each string represents one line in the file. – Visual Vincent Jan 10 '19 at 15:13

1 Answers1

0

I figured I'd turn my comment into an answer. All you need to do is copy and paste the JavaScript yourself because this is valid VB code from 2015:

str = "(function dosomething()
{
    var title = 'Hi';
    return title;
})()"

There's no need to mess around with concatenating anything so there's no real work to do.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Yep and it probably will be the answer, but it would be great to use the JS file if I could. Backup solution would be write it there then copy it over like you suggest. – darbid Jan 10 '19 at 15:09
  • VB and JS code are just text so you could easily enough write a Console app to open a JS file, readf the text and then write out VB code as text to another file. You can also look at using T4 templates for code generation but, if this is a one-off, it may not be worth the trouble. – jmcilhinney Jan 10 '19 at 15:24
  • @jmcilhinney : Or the OP could just read the file from disk (as a string) and inject it into the web browser. Alternatively compile it as a resource. That way he/she still gets to use it as a source code file in VS. :) – Visual Vincent Jan 10 '19 at 15:26
  • @VisualVincent at this stage i don't want to read a whole file. I am looking at adding the whole JS file to the webbrowser which would then mean I could use the JS (reading the whole file) file but am having a few security issues with the website (i don't own) – darbid Jan 10 '19 at 15:51
  • @darbid : _"i don't want to read a whole file. I am looking at adding the whole JS file to the webbrowser"_ - Isn't doing so reading it? Your current method is that you inject the JS code directly into the page, thus if it's located in a file you've got to read the file first to be able to do so. However you can of course link the file by creating an empty `script` element and setting its `src` attribute to the path of the JS file. – Visual Vincent Jan 11 '19 at 06:28