Chrome Extension - How to execute a JS script on page load - javascript

The following is that I'm trying to achieve
Every time 'www.google.com' loads, a script/function triggers. However, the functions itself reloads the page via 'location.reload();'
Basically, an infinite loop of reloads.
Script reloads page -> Script injected -> Script reloads page -> etc.
I want the tab # google.com to keep refreshing while I work/browse the net via other tabs in the same Chrome browser Window.
How should I approach this. I've been looking into background and content scripts but I'm stuck. Assistance is greatly appreciated.

I presume you have no problem getting a content script to run once on page load. I'm not going to help you with that...
You're going to have to
have the content script send a message to the background script
use chrome.alarms.create(string name, object alarmInfo) in the background script
listen for the onAlarm event
use chrome.tabs.sendMessage() to send a message to make the content script reload the page
This is, of course, totally untested!

Related

jQuery script keeps redirecting me from a page I'm trying to edit

I was trying to make an application in SharePoint and wanted to make it so that if you click on a button, it redirects you to a page and when that page loads I wanted it to instantly redirect the user to another page. ( I couldn't get the button to just redirect to the page I wanted on click, so that's why I tried doing it this way. ) I did this using a jQuery / JavaScript script. I'm new to making scripts so I was just testing and I ended up making this script:
<script type="text/javascript">
$(document).ready(function(){
Redirect();
});
function Redirect(){
$(document).load("url");
}
</script>
My problem is now that whenever that page loads, it just loads a blank page. But I can't get to the edit screen because it instantly redirects me to a blank page. I tried turning off JavaScript in Google Chrome but even though I was able to load the page without redirecting, I wasn't able to get to the edit page because it's in a jQuery AJAX drop down menu which obviously also doesn't work if JavaScript is turned off.
I would appreciate any help
EDIT: When I say edit screen I mean the place where I can edit the script. To do that I have to press a button on the page, but I can't do that since the page instantly redirects to a blank page.
Use the webpart maintenance page which allows you to remove and manage web parts without visiting the page, the instructions are as below.
Lets say your page is example.com/sites/default.aspx add the query string ?contents=1 to the page it will take you to the manage web parts page you can remove the script editor web part from there.
The other way is to use SharePoint designer to remove the web part this will also help you achieve the same result.

Keep GreaseMonkey Script Working After Page Navigation

If a Greasemonkey script has functions running, and then navigates to a new page using window.location.assign(), is there a way to keep those functions running, and without the entire script reloading? I'm wondering this since Greasemonkey is a browser plugin, maybe it can keep working no matter what page you navigate to.
I don't think I can use Ajax, or an iframe since it needs to navigate to different domains.
Thank you for any help you can give!
I doubt it is possible. Greasemonkey is a browser plugin, but the script you created for a specific page works only inside this page, not globally. Once you navigate away, the script, part of the page you navigate away from, is killed, like the rest of the page.

How to make Javascript also run after induced button click changes page?

I'm trying to write a script (bookmarklet, really) which has two parts. At the end of the first part, I want to click a button on the page which takes me to a second page. I then want the script to continue running after the second page has loaded. Is this possible?
Every web page load is treated separately by the browser, so there is no way to get a script to continue running where it started off. Here are a few solutions though:
1) Save state to cookies, then read the cookies from the script on the second page to pick up where you left off. For instance, you could save the user's name "John Doe" to a cookie in the first page, then the script in the second page could load the user's name from the cookie. This is probably what you'll want to end up doing.
2) Instead of loading a new page in the browser window, load your new page in an iFrame, and the script running in your outer window won't be interrupted. You can reach inside iFrames with JavaScript as long as they are on the same origin.
You aren't able to load a new page and continue a script from the previous page. But you could ajax load the new page, use the history API to modify the URL, and your script would keep running.

Chrome Extension - Detect Status Code of Page

I have a content script which manipulates a webpage. If the webpage doesn't load (maybe the server gives you an error 500, or chrome can't connect) I want it to retry and refresh the page after a minute or two.
Content scripts aren't injected into the page if chrome can't connect to load the page. Not sure about error 500s, but I know you can't simply detect the status code from within a content script.
How can I, regardless of if the page successfully loads, detect a non-200 status code of a page? I understand that I'll probably have to do this outside of a content script, I'm not sure where I need to do it though.

Reloading Website without stopping JS code

I am trying to run a JS script on a website (not my own) and I want it to refresh the website, in order to check for updates. However, I have only found code online for reloading the entire page (location.reload(true), etc...), which clears any code that I have running through the console. I am new to JS so is there any way to refresh a page and keep the JS code running? Also might there be a way to only reload load a certain portion of the page?
Basically,
Reload website without stopping code
Using jQuery you can easily load any part of a page from a URL using AJAX. To fill the body element with the contents of a URL:
$('body').load('/page');
Your URL can respond with the segment of HTML you want to render, or you can request a full web page and grab just the segment you want buy adding a selector:
$('body').load('/page body');
The page isn't technically refreshed, just the HTML content inside the body (or whatever element you select) is replaced. Any previously loaded header content like JS remains and keeps running.
There is no way to actually refresh the entire page without stopping the execution of the JavaScript code.
For doing updates on the page there would be two possibilities:
Use of AJAX (Asynchronous JavaScript and XML) to check for new updates on the page. There are some very good tutorials out there on the internet – just google »AJAX JavaScript«.
Use of IFRAME. Make a page and stuck all the stuff in it and then put that page in an iframe and then reload the iframe instead of reloading the entire page.
Hope I could help you.

Categories

Resources