Injected Iframe removal on URL change | Chrome Extension - javascript

I am looking for a way to remove iframe that I injected through my content_script. I want the frame to be removed when url of the webpage changes. There is no way to add listener to current page url changes or to use chrome.tabs.onUpdated.addListener in the content script. I can resort to other methods like setting timer that checks url every second or so but I want to know if there is a better way to do this.
This is my first chrome extension. Any help would be appreciated!

Related

Iframe src pointing to wrong URL

My document gives the user a choice of what program (effectively URL) to load in an iframe.
The user may select another program later and the browser puts the prior choice in history.
This allows the user to go back to a prior program that was loaded.
No problem and the onload JAVASCRIPT lets the program know when this happens.
Problem is the onload functions looks up the src attribute of the iframe, but instead of getting the URL of what is now (re)loaded in the iframe it lists what was the last change in the src.
I tried accessing the href of the iframe, but that is protected since it is from another site.
Is there a way to retrieve this. I suspect that using replacestate and popstate might be able do the trick. Any ideas?

How to choose when to fire a content script for Chrome extension?

From my experience, a content script that matches a url in the manifest will fire right away when going on the url. Is there anyway to choose when that content script will fire, and if it is possible to re-fire a content script more than once?
Right now I want to change the text of data generated in a pagination. So page 1 of the pagination would be successfully changed when I go on the url. After clicking on page 2, since the page doesn't refresh when changing the data on the pagination, every other page thats not the first page will remain unchanged
In my extensions a preferred the earliest time possible with run_at to inject the script. In the script itself i check for the things i need.
MutationObserver could be a thing for this`to check for DOM modifications.

refresh Iframe without affecting parent page without iframe id

I am using 2 Iframe in my index page. If any action is preferred from any Iframe, it must reload that Iframe alone. But it overrides my index page as one of the Iframe URL. Can any one help me to fix this issue.
My expected result is working in Chrome but it not working in Internet explorer
Note: Iframe id and name generated automatically
I used the below command to refresh the Iframe window
window.location.reload(true)
window.top.location.reload(true)
screen reference
Try to reload the specific Iframe element because you're probably reloading the whole page (window) right now.
1- First you must take the name for each Iframe with the name attribute.
2- Later try to use the javascript function document.getElementsByName("IframeName") to retrieve the specific Iframe. After that you can take the resulting object (the Iframe) to apply the changes you need (to change the src attribute).
IMPORTANT: Remember that iframes are neither supported by every browsers nor HTML5.
Hope it helps you...

Remove history from External iFrame redirects and links

I have an iframe pointing to an external link that does some redirections as soon as the iFrame loads. This causes added history into the browser and also when users click on links inside the iframe.
This causes a weird user experience because you have extra back history states pointing to the same parent page.
I was wondering if there was a way to prevent the iframe from ever adding history to the browser?
Since the link is external, the short answer is no. If you are in control of the content of the iframe there are a few things you can do. History will be updated any time the src attribute of an iframe is changed after it has been added to the DOM or if the user follows a link in the iframe. With control of the iframe content, when a user clicked a link, you could use postMessage and have the parent window replace the iframe element with a new one. In the same domain, you could just use window.parent.
You can try to use sandbox="" attribute to prevent the iframe from redirecting.
<iframe src="http://example.com/" sandbox=""></iframe>
There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.
Source (MDN)
location.replace() | MDN
http://jsbin.com/coregedoze/2
Here is a link of my code with an iframe, this has no sandbox attribute- as this attribute only works with HTML5 and limited browser version. I rechecked this code and it wont save any history. Only case is if you change your source of the iframe the whole page reloads and makes another entry into history, which can be avoided using sandbox="value" as mentioned in above answer by JAYDEN LAWSON. Another way around is declare your links in your page out of iframe and call them in iframe like traget="iframe_a" , where iframe_a is your iframe name. hope this helps :-)
No, You can do almost nothing with an iframe which has src set to another domain because of XXS

Running Javascript(in Chrome) after I redirect to a new URL on the same tab

I'm trying to make a simple bot that based on some conditions redirects me to a new URL on the same tab and clicks a button automatically. I have everything working dandy but when I get redirected to this new tab my code disappears and everything is reset. The redirect is all on the same domain. By the way I'm putting this code into the console in Chrome if that helps anything.
var a = URL
window.location.replace(a) //Redirects to a given URL and clears the console
$("input[type='submit']").click() //Doesn't get executed due to console being cleared
If there is no "clear cut" way to do this is there anyway I can work around this? If you need anymore information I'll be happy to provide it.
Edit: I tried using iframe to solve this but just threw an error which based off some google searches can't be solved.
Refused to display 'www.tacos.com' in a frame because it set 'X-Frame-Options' to 'DENY'.
Thank you in advance.
You'll need to execute this code in separate statements (execute your last line after the second page loads). Also, it may help to right-click in the console and choose "Preserve log upon navigation".
If you take this code out of the console, it won't work. You may be able to load the new page in an <iframe> and run the javascript against the document in the frame.
When you change the url, your page is redirected so your code stops executing. The solution would depend on whether the page is on your domain, as a solution would be an iframe, but if the url is off-domain, you will run into security issues if you try to fire the button from outside of the frame.
The only way you could get that is by:
Using $("input[type='submit']").click() on the page that will be presented.
Use ajax and replace the current page with the url's content. Be aware of using jquery's live()
You could add a querystring to the url that is parsed on a document load event and does your click action.

Categories

Resources