Greasy Fork is available in English.

Discussions » Développement

Can't get frame title

§
Posté le: 29/08/2014
Édité le: 04/09/2014

Can't get frame title

Firefox 31.0 • Greasemonkey 2.1

  1. Go to https://ixquick.com
  2. Search for anything.
  3. Click a "Proxy" link next to any result. That's the document the script runs on.

If you right-click the lower frame and choose This Frame → View Frame Info, you can see the original document title. Why doesn't the following script manage to fetch it?

var siteframe = window.frames[1];

if (siteframe) {

	// Sometimes the frame's location is about:blank,
	// not that it makes any sense to me.
	if (siteframe.document.location !== "about:blank") {

		var sitetitleone = siteframe.document.title;
		var sitetitletwo = siteframe.document.getElementsByTagName("title")[0];

		if (sitetitleone.length == 0 && !sitetitletwo) console.error("Couldn't get the frame title.");
		else console.info("Success!");
	}

}

I think it's a timing problem. If you @grant GM_log, you get a success message with this code, but it took over 30 seconds for the page I tested:

var siteframe = window.frames[1];
function getTitle(e){
  // Sometimes the frame's location is about:blank,
  // not that it makes any sense to me.
  if (siteframe.document.location !== "about:blank") {
  
  var sitetitleone = siteframe.document.title;
  var sitetitletwo = siteframe.document.getElementsByTagName("title")[0];
  
  if (sitetitleone.length == 0 && !sitetitletwo) GM_log("Couldn't get the frame title.");
  else GM_log("Success!");
  }
}
if (siteframe) {
  siteframe.addEventListener("load", getTitle, false);
}

Yikes. It would be amusingly impractical for a script to take that long to work. I guess I'll just stick to making the bottom frame the top-level frame.

Thank you very much for solving the mystery.

Actually, after having tested this a bit more, I've never seen it take longer than 2 seconds to complete. So I'll be using it after all.

And a follow-up question if I may: why did you prefer GM_log over console.log? The Greasemonkey wiki says it's deprecated and the latter should be used instead.
http://wiki.greasespot.net/GM_log

Re: GM_log, it's just force of habit.

Poster une réponse

Connectez-vous pour poster une réponse.