javascript - Is it possible to search the DOM for a keyword occurring within an iframe and reload page until it's found? -
i have ad displays within iframe on given publisher site around every 1000 loads or so. have no control on host site need way see ad live users see it. i'm trying figure out javascript solution load page, search name of company see if ad loaded (the company name id of div tag loads iframe) , either stop there if finds it, or reload page if not.
i had sort of working running script in console got innerhtml of document body, searched keyword , reloaded page if keyword wasn't found.
two problems though.
- it find keywords outside of iframe.
- it didn't search content of iframe (where actual keyword identify particular ad sits) if set delay or did onload.
secondly, every page refresh, script cleared console.
i know beginner stuff love pointers correct way tackle problem.
thanks far (also, upvoted don't think have necessary cred show publicly)
here's got. created chrome plugin following manifest.json:
{ "manifest_version": 2, "name": "ad refresh", "version": "1.0", "permissions": [ "activetab", "tabs" ], "content_scripts": [ { "matches": [ "<all_urls>" ], "js": ["jquery.min.js", "contentscript.js"], "all_frames": true } ] } i have content-scripts running on urls restrict once things running properly.
for contentscript.js gets injected , runs in each frame, have:
settimeout(function(){ $("[title='3rd party ad content']").attr("id", "dfp"); // "3rd party ad content" title of iframes potentially contain ad , identifying attribute across iframe instances. stick id on there it's easier grab getelementbyid. gets first instance though, need figure out how loop through all. var company = document.getelementbyid('dfp'); if (company == null) { console.log("no hit"); } else { console.log(company); } }, 5000); i'm not worried reloading page, i'm stuck on getting access within iframe.
i unable directly grab element within actual content of iframe jquery $ or getelementbyid etc. however, if run getelementid on iframe , console.log it, includes html inside iframe:
http://i.stack.imgur.com/dfuyt.png
i tried getting innerhtml of iframe element i'd have string , search it, returns iframe element tags, none of inner content. don't know if of makes sense appear on head @ point.
ok, last addition. ad runs script can see under "sources" in inspector. thought "why not run var scripts = document.getelementsbytagname('script'); array of scripts loaded on page? search array see if script , hence ad had loaded , we'd golden." unfortunately though, doesn't include script in array, when it's loaded , visible in "sources" , accurately includes random stripe script that's loading within iframe. bummer...
use
.loadevent ofjqueryknow whetheriframeloaded , readinnerhtmlofiframe body
try this:
$('#id_of_the_iframe').load(function() { var iframecontent = $('body', this.contentwindow.document).html(); console.log(iframecontent); });
Comments
Post a Comment