讨论 » 开发

Can't get frame title

§
发表于:2014-08-29
编辑于:2014-09-04

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!");
	}

}
§
发表于:2014-09-01

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);
}
§
发表于:2014-09-04

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.

§
发表于:2014-09-04

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

§
发表于:2014-09-05

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

发表回复

登录以发表回复。