Have JS-code created in the console persist between page reloads? - javascript

I have some code which I defined in the Chrome developer tools console:
alert("I was just reloaded");
How can I have this code run everytime I reload the browser?
The code is only defined in the console, in Google Chrome in this particular case.
Note, I don't want just the log to persist, I want the code to persist and to rerun everytime I reload the page.

Ok this doesn't answer the question exactly but achievies similiar results.
Solution: Code can't persist between page reloads in the console. Only the console log can persist. However, if you want code to run each time you reload the page, we can use User scripts: Which basically is your own Javascript code, stored in a browser extension.
So what you have to do is to download the appropriate User Script extension for your browser. Most common are GreaseMonkey for Firefox. A tutorial can be found here: http://hayageek.com/greasemonkey-tutorial/
With this extension you can then create your own JS code to run on specific (or all) web sites, which will then persist between page reloads, which achieved exactly what I asked for.

Related

How to open console automatically when any page loads,without pressing F12 or inspect

I trying to open console automatically when any web page loads without using F12 and inspect methods.
Means when we hit any URL console gets open automatically along with loading of page.
It depends on what browser you're using. One way of doing this would be to launch Chrome with the '--auto-open-devtools-for-tabs' flag.
https://developers.google.com/web/tools/chrome-devtools/open#auto
It seems unlikely that this is possible,
check this: https://code.google.com/p/chromium/issues/detail?id=112277
which says: "We only allow explicit devtools opening."
you can refer this link for debugging
Also, you may use remote debugging page which opens devtools in a page. https://developers.google.com/chrome-developer-tools/docs/remote-debugging

How can I run javascript on similar pages?

I want to use chrome snippet to run a javascript code to autofill a series of pages.
The problem is after another page was opened, the code seems stopped.
Instead, I need it run continually.
Like this:
//Page 1 form
document.getElementById('page1Radiobutton').click()
//Click Next
document.getElementById('page1Next').click()
//Page 2 form (need run continually after page 1 was submitted, but now stop at here...)
document.getElementById('page2Radiobutton').click()
document.getElementById('page2Next').click()
I would look into browser extensions such as Tampermonkey (for Chrome) or Greasemonkey (for Firefox)
With either of these monkeys, you can create snippets of arbitrary javascript that will run on any page on the internet automatically.
If you are going to create a script that runs on every page, make sure it exits gracefully if it can't find the elements you're looking for. Otherwise you'll end up with javascript errors when you go to other pages.
Your JavaScript can't really run across pages. Once the user navigates to a new page, all JavaScript is thrown out by the browser for security (among other) reasons.

Google chrome Javascript debugger

is it possible to reload the content of the viewport without reloading the whole inspector.
I would like to make some changes to a javascript file in the debugger then reload the page to see the temp changes I have made (on load), before I commit to the file and upload. Where I'm working at the moment there is caching I can't get round so I have to wait a minute or two to see what my changes do on load.
I have found the page I want to work with under source and can save changes, but really need to see some actions that are fired on the loading of the page.
Is this possible?
Thanks
you can't do that with chrome debugger tool. each JS resource will reload on page load so you will loose your change.
you can proxy tools like Fiddler which will allow you to load script from your local machine while loading a third party website. You can create a copy on your own machine and then use fiddler to intercept the call for that file and send your local copy instead

How to prevent javascript from running twice if same page is opened in two different tabs?

I have a site made in php that calls a javascript file to check for site notifications and then send them as a browser notification (ie Mozilla's Firefox Notifications, Chrome Desktop Notifications, etc.). It works really well, and some users have asked for a chrome notification. I made a basic chrome notification that uses the same code, and it works great for when people aren't using the site. However, the problem is when they're both running at the same time. Users who are on the site and who are using the extension find themselves getting double notifications.
Is there a way to make sure that neither one's code runs if the other is active?
Thanks!
The best way to do this is to mark the alert as read on the server side. That way if I have both Firefox and Chrome open and they go to pull the alert, whoever gets there first will mark the notification as read so that the other doesn't alert it.
You Can set cookie. if one script is running then set cookie. and when you start executing your code then first check if cookie is set? if yes it means another script is running. if cookie is not set then start execution of code.
hope it helps.
Thank you.

How to apply JavaScript changes in Firebug?

I am playing around with a JavaScript code in Firebug and I would like changes to take effect in that page. Especially when there is code inside jQuery's $.ready() function.
Some kind of refreshing the page without losing of what has been edited. Is there any way to do that?
Page changes made via Firebug or via Javascript do not persist from one page load to another. Each time a page is loaded, the original HTML, CSS and JavaScript is parsed and loaded (from cache or from the server). Any prior changes will not be there.
The only way for a dynamic page change to be still present after a refresh is for you to save the changed state to a persistent location and then rebuilt the appropriate page content from that state each time the page is loaded.
But, if you make a change to the page and store some state in a cookie, in local storage or on your server, then you can have JavaScript that runs each time the page loads that gets that state from wherever you stored it and then applies the appropriate change to the page. If you're saving the state on the server (on behalf of this particular user), then you could even have the serve modify the page contents before it is served to the browser.
You can type JavaScript code in the Firebug command line and see changes take effect on the page. You can do the same in the Firefox, Chrome, Opera and Safari DevTools.
Changes to pages done via Firebug do not persist. After a page reload the original sources will be loaded again (from the server or the browser cache).
Currently Firebug doesn't allow you to edit the code of the loaded scripts directly.
Though you can execute JavaScript code within the context of the page by using the Command Line:
Or for longer scripts you can use the Command Editor:
But again, code you executed there will be gone as soon as the page is reloaded.
To make permanent changes to the JavaScript code of a page you need to have access to the server and make them there.

Categories