How do I share Javascript code between files in Windbg preview? - javascript

How do I share Javascript code between files in Windbg preview?
Right now I have several helper methods that I have copied and pasted into different javascript files. I'm not all that experienced with javascript, so my apologies if this is a stupid question.
As an example, let's say I want to use this function in more than one file:
function GetGuid( objectPtr )
{
return ExecuteCommandToString( "dt nt!_GUID " + objectPtr )
.FindLineContaining("{").trim().replace("{", "").replace("}","");
}

I have a common.js which has a few functions that are normally reusable like
host.diagnostics.debugLog()
i first load it using .scriptload
then in other js files I create a var to those functions and use it
see if this helps
contents of common function file
C:\>cat c:\wdscr\common.js
function log(instr) {
host.diagnostics.debugLog(instr + "\n");
}
function exec (cmdstr){
return host.namespace.Debugger.Utility.Control.ExecuteCommand(cmdstr);
}
a js file using the function from common.js
C:\>cat c:\wdscr\usecommon.js
function foo(){
var commonlog = host.namespace.Debugger.State.Scripts.common.Contents.log
var commonexec = host.namespace.Debugger.State.Scripts.common.Contents.exec
commonlog("we are using the logging function from the common.js file")
var blah = commonexec("lma #$exentry")
for(var a of blah) {
commonlog(a)
}
}
actual usage
C:\>cdb calc
Microsoft (R) Windows Debugger Version 10.0.16299.15 X86
0:000> .load jsprovider
0:000> .scriptload c:\wdscr\common.js
JavaScript script successfully loaded from 'c:\wdscr\common.js'
0:000> .scriptload c:\wdscr\usecommon.js
JavaScript script successfully loaded from 'c:\wdscr\usecommon.js'
0:000> dx #$scriptContents.foo()
we are using the logging function from the common.js file
start end module name
00f10000 00fd0000 calc (deferred)
#$scriptContents.foo()
0:000>

Related

load method in THREE.FileLoader only gets the content of index.html

I've set up a very small browser project using create-js-app. I try to load a file called test.txt located in my src directory.
Following is the content of my main.js:
import * as THREE from 'three';
const loader = new THREE.FileLoader();
loader.load(
"test.txt",
function (data) {
console.log(data);
},
function (xhr) {
console.log(
"Loading file : " +
(xhr.loaded / xhr.total) * 100 +
"% loaded"
);
},
function (err) {
console.error(err);
}
);
When running my site in Chrome, I get the content of my index.html file instead of test.txt. I spent some time trying to understand this with no success.
I get this result no matter which file path I specify as first argument of loader.load(), I can even specify a file that is not existing.
Has anybody already faced this issue?
Thanks a lot.
Edit: I'm using Parcel as the bundler.
This issue comes down to internal details of create-js-app, and different web applications may host static resources (i.e. images and other assets that are not compiled source code) in different ways. But generally speaking, the src/ directory is not hosted/deployed/served on the website. If your application has the structure shown here then you probably want to put the .txt file into the public/ directory instead.
Requesting any file that cannot be found at the given URL might be giving you the index page instead, depending on how your site is set up by create-js-app.
I'm updating the answer above: I'm using Parcel. I fixed my project by adding this line:
url = require('test.txt')
The require function makes the browser import the txt file, and it returns the hashed url of the stored file. All I had to do next was to call
loader.load(url, ... )

How do I load antlr/index into ace worker js file?

I am following the instruction given here to integrate antlr4 with ace editor and I have trouble at the step var antlr4 = require('antlr4/index');. The author mentions that here we should use require for nodejs. However, ACE has another require that may cause problems. Thus he loaded another script for nodejs require and load antlr4/index with require in that script.
I tried that one, too. But it always cannot find the script. From the console of my browser, I can see the path it loads the script is:
localhost:4200/./antlr4/index.js and it fails to load it.
I am using Angular 7, and the structure of my project is as follows:
Also, when loading some local javascript file using importScripts, I always fails by giving the local path, however, giving URL from CDN will always work. But importScripts should support local file importing.
Where should I make changes and what else methods should I try?
Here are some of my code:
var ace_require = require;
window.require = undefined;
var Honey = { 'requirePath': ['..'] };
//importScript{"require.js"}
//the script can't be imported through importSctipt so I pasted the
//whole script file under...(not shown here)
var antlr4_require = window.require; // antlr4_require:antlr using nodejs require;
window.require = require = ace_require; // require:ace using its own require
var antlr4, LPMLNLexer, LPMLNParser;
try {
window.require = antlr4_require;
antlr4 = antlr4_require('antlr4/index');
//the browser stuck here reporting error...
} finally {
window.require = ace_require;
}

How to use Docx.js?

i have use docx.js for generate docx file, but generaeted docx doenot open. my generated docx file output is
i have included following js file into my Html page
<script type="text/javascript" src="js/Docx/jszip.js"></script>
<!-- Include main js lib -->
<script type="text/javascript" src="js/Docx/DOCX.js"></script>
and javascript function is
function test() {
//alert("test fn call");
var doc = new DOCXjs();
doc.text('DOCX.js is a free open source library for generating Microsoft Word Documents using pure client-side JavaScript.');
//doc.text('It was developed by James Hall at Snapshot Media.');
var output = doc.output('datauri');
}
when i click button this function will be calling, and also i want to add datatable as a content of word document so how to add content using Docx.js?
Probably you are trying to use the docx.js without server, that will not work because it can't load the necessary files.
if you are executing the code on a server then edit the path of '/blank/' somewhere in the docx.js file
$.ajax({
url: '/blank/' + files[file],
complete: function(r) {
//file_data[this.url.replace(/blank_/, '')] = r.responseText;
zip.add(this.url.replace('/blank/', ''), r.responseText);
file_count_current ++;
if (file_count == file_count_current) {
doOutput();
}
}
});

JavaScript File For Each HTML File?

I have a javascript file that handles the scripting of several HTML files.
I started running into issues as everything is just written out, and nothing is called in functions (so all AJAX calls for instance are called at all pages, even though each call should only be called at one page one).
What is the best way to go about improving/fixing this? Here are the two ways that I thought of doing it:
Create a separate JS file for each HTML file
Put all JS code in functions and call each function respectively
I am leaning towards putting all my code in functions. However, when I did:
<script src="the_source">
call_function
</script>
That didn't seem to work. I put that right before where the responsible HTML was. I originally wrote it in Haml as follows:
%script{:src => "src"}
call_function
How can I get this HTML function calling working?
Or is separating them whereby each HTML file has a different JS file considered a better solution? The problem is that there is a lot of shared code between them. To solve this, I can create a different file with the shared code in objects which can be called from the other JS files as needed.
What is the cleaner approach/solution to this problem?
To call function use:
call_function();
Edited:
<script src="the_source"></script>
<script >
call_function();
</script>
where "the_source" is path to your js file
End edit
To separate the logic in every page i would just create some settings object and put there flag for cases you want to have separate:
//this code should be on every page you need your javascript file
<script src="the_source"></script>
<script>
var settings = {
mode:'doSomething1'
}
call_function(settings);
</script>
in your js file do things depending on the settings.mode value:
function call_function(settings)
{
switch(settings.mode){
case: 'doSomething1':
//code for case1
break;
case: 'doSomething2':
//code for case2
break;
default:
//code for default case
break;
}
}
This depends on your hosting plan:
If it's a dedicated or semi-dedicated server, I would prefer to have a shared JavaScript file (for performance).
If it is any other kind then separate file is faster as it distributes the load of requests in different JS files.
If you don't want to create separated files as it will be messy, I suggest to do the following:
<script src="somewhere">
var page = "<?PHP echo basename($_SERVER['PHP_SELF']); ?>";
Run_Function();
</script>
In somewhere:
function Run_function(){
switch(page){
case "1.php":
//Do Something
break;
case "2.php":
//Do Something else
break;
default:
alert("Unregistered webpage");
break;
}
Good Luck :D
EDIT:
Try up this function to get the file name from the URL:
function FileName(){
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
return filename:
}
UPDATE:
Please check this improved function of the above:
function FileName(){
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
if(filename == "" || filename == "Undefined"){
filename = "index.ruby";//or any other extension
}else{
var filename = filename.replace(/%C3%84/g, "Ä");
var filename = filename2.replace(/%C3%96/g, "Ö");
var filename = filename2.replace(/%C3%9C/g, "Ü");
var filename = filename2.replace(/%C3%A4/g, "ä");
var filename = filename2.replace(/%C3%B6/g, "ö");
var filename = filename2.replace(/%C3%BC/g, "ü");
}
return filename:
}
This function might help more people and it is more flexible to use.
I would put your JS code into several different files, one for each page.
This can help your site run quicker (admittedly not by much) because the browser isn't running through tons and tons of code to find the required function.
Also, (and this is just a personal opinion) , a JS file with just a long list of short functions is quite ugly and seems less efficient.
And yeah, include an extra file with the shared code on. Try to make sure nothing is repeated unnecessarily in the files!
Good luck! :)

Including a javascript file inside a javascript function

I have a need to include a javascript file inside another javascript function. I dont have any html file. I am executing the javascript code inside a .NET program like this
MSScriptControl.ScriptControl js = new MSScriptControl.ScriptControl();
js.AllowUI = false;
js.Language = "JScript";
js.Reset();
js.AddCode(#"
function test(x)
{
return x+10;
}
");
I need to reference a javascript file inside the function test. Can someone help me out?
Thanks
There are no such instruction like 'include' in javascript language. So, to accomplish what you want you have to read another script content and concatinate it with the rest of code;
The code would be something like that:
var includedScript = ReadScriptFromFile("myfile.js");
var includedScript += #"function (x) { return x + 10";
js.AddCode(includedScript);

Categories

Resources