Changeset 674

Show
Ignore:
Timestamp:
03/10/08 11:33:13 (9 months ago)
Author:
oyasu..@gmail.com
Message:

Show user scripts that run for the top frame at the bottom of the monkey menu, and other scripts which run in sub-frames only (formerly not seen at all) on top, separated with a horizontal rule.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/chrome/chromeFiles/content/browser.js

    r652 r674  
    429429 
    430430function GM_showPopup(aEvent) { 
     431  function urlsOfAllFrames(contentWindow) { 
     432    function collect(contentWindow) { 
     433      urls = urls.concat(urlsOfAllFrames(contentWindow)); 
     434    } 
     435    var urls = [contentWindow.location.href]; 
     436    Array.prototype.slice.call(contentWindow.frames).forEach(collect); 
     437    return urls; 
     438  } 
     439 
     440  function uniq(a) { 
     441    var seen = {}, list = [], item; 
     442    for (var i = 0; i < a.length; i++) { 
     443      item = a[i]; 
     444      if (!seen.hasOwnProperty(item)) 
     445        seen[item] = list.push(item); 
     446    } 
     447    return list; 
     448  } 
     449 
     450  function scriptsMatching(urls) { 
     451    var matching = []; 
     452    for (var i = 0, script = null; script = config.scripts[i]; i++) 
     453      for (var j = 0; j < urls.length; j++) 
     454        if (GM_scriptMatchesUrl(script, urls[j])) { 
     455          matching.push(script); 
     456          break; 
     457        } 
     458    return matching; 
     459  } 
     460 
     461  function appendScriptToPopup(script) { 
     462    var mi = document.createElement("menuitem"); 
     463    mi.setAttribute("label", script.name); 
     464    mi.setAttribute("value", i); 
     465    mi.setAttribute("type", "checkbox"); 
     466    mi.setAttribute("checked", script.enabled.toString()); 
     467    popup.insertBefore(mi, tail); 
     468  } 
     469 
    431470  var config = new Config(); 
    432471  config.load(); 
    433472  var popup = aEvent.target; 
    434   var url = getBrowser().contentWindow.document.location.href
     473  var tail = document.getElementById("gm-status-no-scripts-sep")
    435474 
    436475  // set the enabled/disabled state 
     
    444483  } 
    445484 
    446   var foundInjectedScript = false; 
     485  var urls = uniq( urlsOfAllFrames( getBrowser().contentWindow )); 
     486  var runsOnTop = scriptsMatching( [urls.shift()] ); // first url = top window 
     487  var runsFramed = scriptsMatching( urls ); // remainder are all its subframes 
     488 
     489  // drop all runsFramed scripts already present in runsOnTop 
     490  for (var i = 0; i < runsOnTop.length; i++) { 
     491    var j = 0, item = runsOnTop[i]; 
     492    while (j < runsFramed.length) 
     493      if (item === runsFramed[j]) 
     494        runsFramed.splice(j, 1); 
     495      else 
     496        j++; 
     497  } 
    447498 
    448499  // build the new list of scripts 
    449   for (var i = 0, script = null; script = config.scripts[i]; i++) { 
    450     incloop: for (var j = 0; j < script.includes.length; j++) { 
    451       var pattern = convert2RegExp(script.includes[j]); 
    452       if (pattern.test(url)) { 
    453         for (var k = 0; k < script.excludes.length; k++) { 
    454           pattern = convert2RegExp(script.excludes[k]); 
    455           if (pattern.test(url)) { 
    456             break incloop; 
    457           } 
    458         } 
    459  
    460         foundInjectedScript = true; 
    461  
    462         var mi = document.createElement('menuitem'); 
    463         mi.setAttribute('label', script.name); 
    464         mi.setAttribute('value', i); 
    465         mi.setAttribute('type', 'checkbox'); 
    466         mi.setAttribute('checked', script.enabled.toString()); 
    467  
    468         popup.insertBefore(mi, document.getElementById("gm-status-no-scripts-sep")); 
    469  
    470         break incloop; 
    471       } 
    472     } 
    473   } 
    474  
     500  if (runsFramed.length) { 
     501    runsFramed.forEach(appendScriptToPopup); 
     502    var separator = document.createElement("menuseparator"); 
     503    separator.setAttribute("value", "hack"); // to get removed in the loop above 
     504    popup.insertBefore(separator, tail); 
     505  } 
     506  runsOnTop.forEach(appendScriptToPopup); 
     507 
     508  var foundInjectedScript = !!(runsFramed.length + runsOnTop.length); 
    475509  document.getElementById("gm-status-no-scripts").collapsed = foundInjectedScript; 
    476510}; 
  • trunk/src/chrome/chromeFiles/content/utils.js

    r652 r674  
    497497  } 
    498498}; 
     499 
     500function GM_scriptMatchesUrl(script, url) { 
     501 for (var i = 0, glob; glob = script.includes[i]; i++) { 
     502   var re = convert2RegExp(glob); 
     503   if (re.test(url)) { 
     504     for (var j = 0; glob = script.excludes[j]; j++) { 
     505       re = convert2RegExp(glob); 
     506       if (re.test(url)) 
     507         return false; 
     508     } 
     509     return true; 
     510   } 
     511 } 
     512 return false; 
     513}