Is it possibile to stop a script from another script? - javascript

I started to make a simple js debugger, and I was wondering if there is a way to stop an already running script if either it isn't responding or I want to stop it's setInterval() loop where we don't know the id of the loop.
I've noticed in the Chrome DevTools that every document has it's own script collection under document.scripts, but I couldn't find anything like a list of intervals (eg:window.intervals) or any response if any function is running,so I can't really determine if a function needs to be killed.
This could be useful when you want a webpage scanner extension or a debugger thet can refresh the page when the script is frozen.
Edit:
The list of intervals has already been asked and solved in How can I get all timers in javascript question.
Now the only question remains is: How can I determine if a script is frozen so it or the page can be killed?

Related

JavaScript code runs 3-4 secs and disables browser during that

I'm just wondering why my code takes so long with Internet Explorer 11. I have a PHP page which calls a function. That function returns a very long string. The string is actually JavaScript code which has 400 rows.
Let's assume the returned string is like this:
<script>
document.getElementById('pka1').innerHTML = 'gddgdgd gsdg gdsgs';
document.getElementById('pka2').innerHTML = 'gg gdsgdsggg gsg';
document.getElementById('pka3').innerHTML = 'fdfd ffdsf dfss ff';
...
document.getElementById('pka398').innerHTML = 'hfhhfd hdhfh fhdfd';
document.getElementById('pka399').innerHTML = 'ggjggfgjgh h ffhfh';
document.getElementById('pka400').innerHTML = 'fssfs ffsafsa eefg';
</script>
When that string has been returned, I use jQuery to run the code. The returned string is stored to variable named as data. So, I run the following command:
$('#pka').html(data);
After that Internet Explorer and other browsers will run the previous JavaScript code which consists of 400 rows and modifies the HTML code of 400 divs.
Mozilla Firefox does this very fast, but Internet Explorer spends too much times - even 3-4 seconds. While the script is running, the webpage is disabled (I do not know why) and I cannot click hyperlinks while the script is running. When using Firefox this takes 0.5 secs, but IE is very slow.
How could I speed up the process and make the page not to be disabled when a browser runs the JavaScript code from the div which id is pka?
While the script is running, the webpage is disabled (I do not know why) and I cannot click hyperlinks.
Simply because that's how it works.. Java script execution model makes the a function or code block runs to complete before it can process other..
So if there is a long one, user interaction will be blocked..
How could I speed up the process
Well, you have to rethink about the 400 divs.. this is too much.
Also try to divide the change into functions as each function is processed alone. it will make it less laggy.
I am not sure about using web workers in manipulating the dom, if it's beneficial or even possible.

Save and load entire web page state (Visual HTML, And javascript Variables)

So lately I've been working on this project, which will end at the end of January (end of this week).
http://www.nikollr3.three.axc.nl/ (this is a preview of the program a few months ago, but u get an idea)
We have started testing the website and noticed that it was sometimes making Firefox crash.. since Firefox' error or crash reports were never of any use (no useful information found), I have no idea how to fix this error. And a possible solution would be to have a save / load button on the web page, which can save or load a state (and for example autosave state untill it crashes) then when it crashes you would be able to reload the last state and continue working on the project. By saving a 'state' I mean recreating the EXACT moment as to when the save button was clicked, thus same javascript variable values and same HTML looks.
I have found little things about this on the internet, the only one relevant being for android phones,.. Any ideas or implementations?
write all your variables and "HTML looks" (attributs) to a XML-file. load (if (exist(XML)pseudo-code) all necessary data onPageLoad

How to see the steps of calling javascript functions in Firebug

I'm working with JVectorMap and I have made some changes on the code as well.
the program works well but I don't know why when I'm rendering the map two request for rendering the page send to my servlet.
everything seems ok and I didn't call my rendering servlet code twice but I don't know why two request come to my servlet.
I supposed that may be a refresh request while rendering, somewhere but I couldn't find any, I want to check the process of rendering my page step by step to figure out what the problem is but It seems debugging the javascript is a cumbersome task and I don't know how in firebug I can check each part of my code step by step as I have some javascript file imported as well.
do you have any suggestion or idea ?
On the script tab of firebug click at the line where you want to stop execution.
Alternatively you could insert debugger; instruction in your code.
Then use buttons on the top of script tab. If you don't know how - google for "debugging javascript with firebug".

Page refresh in case of javascript errors

I have some pages running javascript for displaying updates of different measure values. These stuff seems to run fine. But - there is always the risk that javascript can crash in case of a fault (especially after running some hours)
So I want to implement a simple kind of watch dog. One of my ideas is to use a meta-refresh-tag. The Browser will reload the site after xy minutes and all javascripts will be reinitalized.
Of course I do not want to refresh the site if no error occured and want to reset the refresh timer using javascript. As long js runs the timer will be resetted periodally. If javascript crashs the meta-refresh-timer counts down to zero and the page will be reloaded.
Reading stackoverflow postings I have found some answers that says a reset of the meta-timer by javascript is not possible. Do you know a better way to implement the watchdog? Using javascript ittself for refreshing is useless: If the script crashs it will never fire a reload event..
Ideally you should properly handle the error instead of just reloading the page. However you could use the window.onerror event like this:
window.onerror = function() {
location.reload();
}
More information about onerror here:
https://developer.mozilla.org/en/docs/DOM/window.onerror

Firefox Extension: Access the DOM Before It's Loaded

I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. My Javascript code will basically control whether or not the page's Javascript code can be executed or denied.
I first started out by trying to follow this answer, but I couldn't really figure out how to get it to work and realized I was relying on onDOMContentLoaded, which loads after the Javascript has already executed.
I then turned my attention toward XPCOM, but once again didn't really understand what the Firefox tutorials were telling me.
I've recently been trying to make an extension through Firebug, but I seem to hit the same problem... only having access to the Javascript after it's been parsed/executed. Here's the resulting code that I wrote. I think if I could access the file's objects in the onExamineResponse event, my problem could be solved, but I don't know how to do that... I'm talking about this code:
BeepbopListener.prototype = {
onRequest: function(context, file) {
...
},
onExamineResponse: function(context, file) {
FBTrace.sysout("onexamineresponse " + file); // this returns something like
// '[xpconnect wrapped (nsISupports, nsIHttpChannel, nsIRequest, nsIUploadChannel, nsITraceableChannel, nsIHttpChannelInternal)]'
// but I don't know how to access those elements...
var pattern = /\.js$/;
if (pattern.test(file.href) && FBTrace.DBG_BEEPBOP) {
FBTrace.sysout("ONEXAMINE DOESN'T EVEN GET IN THIS IF SO YOU WON'T SEE THIS");
}
},
...
};
So my question is... is there a tutorial out there that shows me how I can get access to all Javascript code on a page before it's executed? Also, if anyone has any helpful insight, I'd love to hear it. Oh, and if y'all need more code from me, just let me know, and I'll post it.
You can access a new document before any JavaScript code runs by listening to the content-document-global-created observer notification. However, the document will be empty at this point and JavaScript code will run as soon as the parser adds a <script> tag - you cannot really prevent it. Here are the options to control script execution that I am aware of.
1) Disable all JavaScript for a window using nsIDocShell.allowJavascript:
wnd.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShell)
.allowJavascript = false;
This is an all or nothing approach. Note that JavaScript stays disabled even when a new document loads into the same frame.
2) Implement the nsIContentPolicy interface in an XPCOM component and register it in the content-policy category (via nsICategoryManager). Your shouldLoad() function will be able to block scripts selectively - but it will only called for external scripts (meaning <script src="...">), not for inline scripts on the page.
3) Use JavaScript debugger service to intercept script execution. You could use jsdIDebuggerService.interruptHook to step through JavaScript execution and abort the script whenever you like. But that would slow down JavaScript execution very significantly of course. At the very least you should use jsdIDebuggerService.addFilter() to restrict it to a particular document, otherwise you will slow down the entire browser (including browser UI).
I'm trying to create a Firefox extension that fires my Javascript code before any of the current page's Javascript is fired. My Javascript code will basically control whether or not the page's Javascript code can be executed or denied.
Start by completely preventing the document from getting parsed altogether then on the side, fetch the same document, do any processing on this document and then inject the resulting document in the page. Here is how I currently do just that https://stackoverflow.com/a/36097573/6085033

Categories