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.
Related
I'm trying to run the collection (GetEmails) script on our site, aventure.vc. It's a website built using Webflow.
THE ISSUE: After moving the Retention script to GTM, we have received multiple emails from Retention informing us that no data has been collected for 3 or more days since it was was moved to GTM.
How can we get the script to fire consistently on our page through GTM?
This is the tag configuration using the code snippet provided to us for collection by Retention.com
https://imgur.com/TtrftmZ
The above tag is present in the live version of the container and is shown to be firing successfully every time when checked using the "preview" option in GTM.
Viewing the data on the Retention dashboard confirms that no data is being collected on most days, but on a few days a small number of entries are being collected.
This Indicates that the script is firing succesfully sometimes through GTM but it is not consistently firing for everyone that visits the site.
As a next step, on the aventure.vc website we used the "inspect" option on chrome, to view the site elements tab.
There we use the "ctrl-f" search option to manually look for the GetEmails collection script to see if it has been loaded into the site succesfully by GTM.
Upon doing this we observed that the GetEmails script does show up in the site elements sometimes,
but in most cases when the site is loaded, the script does not appear when we look for it manually.
TL;DR
The GetEmails script is firing everytime when put in the headers tag through webflow for our site aventure.vc,
but when trying to fire the same script through GTM, the script is missing from the site elements most of the time. Indicating the script does fire successfully sometimes.
How can we get the script to fire consistently on our page through GTM?
You need to do more debugging.
Make sure you've published your GTM workspace.
Make sure GTM loads the CHTML tag on the page whenever you test it.
Check the Network tab in the dev tools to see if the call is being sent to the vendor's endpoint.
Check your all pages trigger. Make sure that real pageview occurs. It will not fire on SPA history changes.
Finally, use the GTM preview debugger for your GTM logic debugging. It will give much more insight into what's happening.
I am building some JS to install on people's websites. I am trying to detect if browser going away from page my script installed on. It would be easy to listen beforeunload event, but what if my script was loaded after user click on some link?
The events order is:
browser loads page A with my script installed
my script started to load
user clicks on link to page B
browser started to load page B
my script is loaded to page A ← How can I detect browser location is changing right now?
You can't detect this in this specific situation. The act of navigating from page A will stop your script from loading, even before page B has loaded.
The relevant parts of the spec are 7.8.1 Navigating across documents which details what happens when the browser is asked to navigate (the link to page B being clicked in your example). Step 11 says:
Abort the active document of browsingContext.
The abort steps make it clear that any still-in-progress fetching or parsing of the active document are simply discarded.
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!
I am building a Firefox add-on using the addon-sdk.
The add-on has one button that displays a panel on click. The panel has a content script running it. Now, I need the panel to look different depending on the user's current tab and occasionally show an external url.
So, the main.js script tracks the current tab of the user and sends messages to the content script using panel.port.emit() and the content script changes the HTML of the panel to match whatever is needed.
However, as I mentioned, some times the panel is just showing an external url, so I need to reset it back to its original url. This is my code:
function panelListMode(currentTab, data){
panel.resize(350,300);
//This is the line causing the trouble
panel.contentURL = self.data.url("panel.html");
console.log("sending message");
panel.port.emit("generateList",data);
}
If I emit the message after changing the contentURL the content script does not seem to receive a thing. I now that specific line is the one causing the problem because if I comment it the content script receives the message.
Something tells me this is because the panel needs to load the DOM again and I need to wait until it is ready before the content script is able to do anything. But... aren't content scripts something apart from just included scripts?
As far as I am able to tell the panel has no "ready" event of sorts that I can listen to for emitting the message.
What am I missing?
Edit: After some experimentation I have been able to find some more. If I copy panel.html to the same directory as panel2.html and change my function so it goes:
function panelListMode(currentTab, data){
panel.resize(350,300);
//This is the line causing the trouble
panel.contentURL = self.data.url("panel2.html");
console.log("sending message");
panel.port.emit("generateList",data);
}
The problem is gone. I have experimented a bit and it seems that when I try to change the contentURL to the html that is already set in the panel the content script stops working all together.
This is some really odd behavior... have I encountered a bug?
As far as I am able to tell the panel has no "ready" event of sorts that I can listen to for emitting the message.
I think it does, at least internally it seems to but I'm not sure whether they're exposed.
But even if it doesn't you can simply fire your own port message from the content script once it has been attached and registered its own listeners. Upon receiving that message in the addon you can then resume using the port on that side.
I want my Chrome extension to rerun a contentscript for all open tabs prior to viewing the popup window to gather the most recent data about each webpage (it sends a message to the background page). My problem is that in order to get full access to each iframe, I need to set allframes to true. This means that prior to showing the popup window, I need to wait until I have received a message from each frame.
My problem is that I am not sure how to determine how many frames there are. One approach I looked at would be to detect the frame count from within the contentscript, but there is an open bug which indicates that you can't request that right now. Another is to just wait for a number of responses equivalent to what I got when I first navigated to the page, but it is possible that due to AJAX calls the number of frames increased since then. Finally, I was hoping that the response to chrome.extension.SendRequest would include such information but it does not.
Your help is appreciated.
I suggest that you always run the content script from the manifest, so that the content script will run every time a new frame loads. Then, have the content script open a port to the extension, so that every open frame has a port to the background. Then it's up to the background page to keep track of all the open ports for each tab, and send messages to the desired ports.