views:

99

answers:

2

I need help getting a Grease Monkey with JQuery Script to run on a broken site.

I'm trying to get the following GM script to run, but the page I want it to work on has a JS error and my JS does not get executed.

// ==UserScript==
// @name          BILL INFO PAGE ALTER
// @namespace     http://jenkinslaw.org
// @description   Alter the web page in order to pretty print
// @include       http://www.legis.state.pa.us/cfdocs/billinfo/bill_history.cfm?*
// @require       http://code.jquery.com/jquery-1.4.2.min.js

// ==/UserScript==
*/
(function() {

  //Make a copy of the bill table
  var bill_table = $('.main_table').clone();

  //empty the whole lot
  $(body).empty();

  //append the bill back to the dom.
  $(body).append(bill_table);

}());

Thanks!

D

Progress:

I agree with @mkoryak this is an impossible problem to solve with GM. So I'm dropping it and using a Firefox extension instead (hopefully it wont run into the same issue).

I'll be following the example I saw on another post here on OS: http://stackoverflow.com/questions/491490/how-to-use-jquery-in-firefox-extension

I was able to get it working but with a slight modification from the example shown:

(As an aside, I used the Firefox Extension Wizard to get a basic framework of the extension set-up easily and quickly).

jQuery.noConflict();
(function($){

billinfo = new function(){};
billinfo.log = function(){ Firebug.Console.logFormatted(arguments,null,"log"); };
billinfo.run = function(doc,aEvent) {

  // Check for website
  if(!doc.location.href.match(/^http:\/\/(.*\.)?legis\.state\.pa\.us\/cfdocs\/billinfo\/bill_history\.cfm\?(.*)?$/i)) return; 

  // Check if already loaded
  if(doc.getElementById("plugin-billinfo")) return;

  // Setup
  this.win = aEvent.target.defaultView.wrappedJSObject;
  this.doc = doc;

  //Make a copy of the bill table
  bill_table = $('.main_table', doc).clone();

  //empty the whole lot
  $('body', doc).empty();

  //append the bill back to the dom.
  $('body', doc).append(bill_table);      
}; 

// Bind Plugin
var delay = function(aEvent){ var doc = aEvent.originalTarget; setTimeout(function(){ billinfo.run(doc,aEvent); },1); };
var load = function(){ gBrowser.addEventListener("DOMContentLoaded", delay, true); };
window.addEventListener("pageshow", load, false) 
})(jQuery);
+2  A: 

You cant do it.

If there is a javascript error, your code (which executes last) will never execute.

I have looked far and wide for a solution for this, but was never able to find it.

mkoryak
Do you know if a firefox extension would run into the same problem?
DKinzer
i dont know, but my guess would be that it would.
mkoryak
@DKinzer: if you are going to try the extension route, you should look into compiling a GM plugin into an extension. i think this is possible, it might save you a step or 2
mkoryak
+1  A: 

GM and jQuery 1.4.* currently fail to co-exist due to an error in the eventSupported function.
Therefore, you can use the 1.3.* jQuery or include a modified 1.4.2 version directly in your script, such as the one suggested here.
Since you have chosen to take the extension path, this is irrelevant to you, but I still post this for others with similar issues who might stumble upon this in the future.

MasterAM
Thanks, this is actually still useful to know. Though I did solve my problem via the extension route already.
DKinzer