User script to help with making/debugging themes

User script to help with making/debugging themes

Helen Foster -
回帖数:13
Core developers的头像 Documentation writers的头像 Moodle HQ的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像
A message posted on behalf of my colleague, Andy:

Hello!

While working on a moodle theme I thought it might be useful for debugging CSS if I was able to select an element in the page and then examine the tags, classes and id's that could be used in writing the CSS selector for that element. For example it might be helpful investigating problems with specificity, or if you're just learning your way around the layout of moodle (or any other site).

So I wrote a short user javascript for use with opera and the greasemonkey firefox extension to act as a simple document inspector - please check screenshot below.

On a page where the script is running, pressing the shift key while clicking on a section of the page will make a small box appear, showing the path through the DOM tree, from the body tag down to the element you clicked on. At each step through the tree it displays the tag name and any classes or id's on the element.

The box can be dragged around the page or cleared by holding down the shift key while clicking on the box.

Note: there may be other tools that perform a similar task, certainly for mozilla browsers - the display id/classes option on the developer toolbar springs to mind, but I found it can make the display too cluttered and it doesn't clearly show the elements position within the document tree. I find this script helpful, hopefully someone else will too. smile

If you're interested in trying it out here's the installation instructions:

For Firefox

Install the greasemonkey extension from http://greasemonkey.mozdev.org/ (at the time of writing greasemonkey 0.3.5 is the recommended version).

After installing the extension restart the browser. If you look in the tools menu you should see a few extra options, including install user script (although this one won't be active yet).

Go to http://www.altoncollege.ac.uk/misc/debugcssselector.user.js

Review the script (to check it isn't malicious wink) then choose tools > install user script.

You should then be prompted to set up the sites you want to apply the script on. By default I set this to be http://localhost/moodle* - that is any file within the moodle directory on the local server. Change this to apply the script to whatever sites you want.

For Opera 8

Go to tools > preferences > advanced tab, then select the content option from the list of sections and click the JavaScript options button. In the dialog box that appears fill in a folder (e.g. c:\userjs) in the "my javascript files" field.

Next download the script from http://www.altoncollege.ac.uk/misc/debugcssselector.user.js and save it in the folder you specified for user javascript files.

The script is currently set up so it will only be applied to files coming from http://localhost/moodle. Assuming the sites you'd want to use it on are different you'd need to edit the script to change this.

At the start of the file there is the following text:

// ==UserScript==
// @include http://localhost/moodle*
// ==/UserScript==


Change the address in the @include line to target whatever sites you like. For further information on setting up the script see http://www.opera.com/support/tutorials/userjs/using/
附件 moodle_css_debug.gif
回复Helen Foster

Re: User script to help with making/debugging themes

Mike Churchward -
Core developers的头像 Plugin developers的头像 Testers的头像
Oh, man! Why didn't you post this earlier in the week?

I just spent three hellish days picking my way through the DOM model of the new themes in order to figure out how to get my old themes to look right. This is *exactly* what I needed... sad

Thanks. I will use this a lot. This is better than the one in the developer tools extension. cool

mike
回复Helen Foster

Re: User script to help with making/debugging themes

Eugene Ritter -
That's awesome! Thanks you just saved me a billion hours of labor hunting down css paths!
回复Helen Foster

Re: User script to help with making/debugging themes

seaghan moriarty -
Fabulous work Helen - this will prove very useful - thank you smile
回复seaghan moriarty

Re: User script to help with making/debugging themes

Helen Foster -
Core developers的头像 Documentation writers的头像 Moodle HQ的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像

Thanks everyone for your replies.

No doubt you can tell that Andy is a real treasure to work with! big grin Hopefully, all your nice comments will encourage Andy to be brave in future and post on moodle.org himself! wink

回复Helen Foster

Re: User script to help with making/debugging themes

Julian Ridden -
*starts chant* Post!! Post!!  Post!! Post!!

Simple but ingenious work. Keep bringing these gems out for the rest of us :D
回复Julian Ridden

Re: User script to help with making/debugging themes

Andrew Walker -
OK i'll post something big grin 

I've written a slightly updated version. The big(ish) new thing is now when you display the list of elements you can click on any of the tag names in the list. Clicking the tag name makes a textfield appear, typing a CSS property name into this textfield - then pressing enter - should show the value of that property for the chosen element. (This was inspired by not being able to spot which element was causing a table in moodle to have a white background wink)

the drag and drop works slightly more smoothly and it should also allow you to select the list of elements it displays (so they can be copy and pasted) without starting dragging the box again.

http://www.moddular.org/log/css-inspector

Has more details (particularly regarding slight differences in behaviour between Firefox and Opera) and also a couple of screenshots.

I hope it's useful for someone smile
回复Andrew Walker

Re: User script to help with making/debugging themes

Jan Dierckx -

Andrew,

This script has been very useful to me trying to grasp all of this theme and css stuff. I will certainly download the new version!

Thanks!

Jan

回复Jan Dierckx

Re: User script to help with making/debugging themes

Andrew Walker -
Thanks Jan!

I should probably mention this new version needs to replace the original - I think there would be some conflicts if they were running side-by-side 微笑
回复Andrew Walker

Re: User script to help with making/debugging themes

sina salehi -

Hi Andrew

Is it possible to use this script in IE ?

回复sina salehi

Re: User script to help with making/debugging themes

Andrew Walker -
Hi Sina,

There are programs available for internet explorer that can give it capabilities similar to greasemonkey in firefox and opera's user javascript, Turnabout looks a popular choice - it's available from http://www.reifysoft.com/turnabout.php

However this script would not be able to run in internet explorer, it should be possible to get it working though (e.g. adding events using a combination of IE's attachEvent alongside the standard addEventListener etc...) If you'd be interested in using it I can try to make these changes.
回复Andrew Walker

Re: User script to help with making/debugging themes

Josep M. Fontana -
Andy,

Great contribution. I was trying to use it but something about 'greasemonkey' is giving me trouble. I know this is not a problem with your script but I don't have anywhere else to turn. I thought maybe you or somebody else who is knowleadgeable can give me a hand.

After installing 'greasemonkey' and installing your script in greasmonkey I noticed that nothing really happened. In fact, I wasn't able to see 'greasemonkey' anywhere even though in 'tools' it says that it is installed. I turned on the JavaScript console in Firefox and I saw the following error:

Failed to launch editor: [Exception... "Component returned failure code: 0x80520003 (NS_ERROR_FILE_EXECUTION_FAILED) [nsIMIMEInfo.launchWithFile]"  nsresult: "0x80520003 (NS_ERROR_FILE_EXECUTION_FAILED)"  location: "JS frame :: chrome://greasemonkey/content/utils.js :: openInEditor :: line 151"  data: no]

Can anybody help me solve this problem.

Thanks in advance.

Josep M.
回复Josep M. Fontana

Re: User script to help with making/debugging themes

Andrew Walker -