About user scripts in beta 3
Wednesday, 16. March 2005, 21:22:11
Hallvord introduced Opera's latest surprise for tweakers in his Journal.
What is a user script
Basically it is a script from a local file that gets executed before any other scripts (or event handlers) on the page, once in every document.
I've collected some more or less interesting scripts that I'd like to run on all pages, and on specific sites, in this user script: <http://people.opera.com/rijk/opera/scripts/user.js>. I'm not yet using any versioning there BTW, it can be a different script tomorrow, or in an hour. The script is set up to easily add extra functions, and enable/disable them for all or for specific sites when you want. Don't expect me to explain specific functions, because I'm not a proficient JavaScripter myself. I'd say: experiment, share, and file bug reports if necessary.
Enabling user script in Opera 8 beta 3
[User Prefs]
User Javascript=1
User Javascript File=C:Program FilesOperauserjs.js
Security
The script has exactly the same restrictions as a script on the page, except that it can access some User JS specific functionality (we'll get back to you on that) and that it can read the text property on any script element (normal scripts can't read the text property of external scripts loaded from other servers than the document.)
The additional privileges are available when the User JS file is initially executed and when a User JS event is being handled. Global functions defined by User JS but called from a script in the page have no special privileges.







non-troppo # 16. March 2005, 21:22
You have an error in your example path: User Javascript File=Crogram FilesOperauserjs.js
/me goes off to read through your user.js
jor # 16. March 2005, 21:22
Rijk # 16. March 2005, 21:22
izzyo # 16. March 2005, 21:22
function yahooNewsCleanser(){
//document.body.innerHTML=document.body.innerHTML.replace(/ (<a(.*)a>)/g,'');
document.body.innerHTML=document.body.innerHTML.replace(/ ((.*)news(.*) - (.*)web sites(.*))/g,'');
}
izzyo # 16. March 2005, 21:22
and try it here, for instance: http://story.news.yahoo.com/news?tmpl=story&cid=519&ncid=716&e=4&u=/ap/20050317/ap_on_re_us/laci_peterson
izzyo # 16. March 2005, 21:22
Rijk # 16. March 2005, 21:22
scipio # 16. March 2005, 21:22
scipio # 16. March 2005, 21:22
I've found the useful function called bodyIDforAll() in your user.js. Can you perhaps explain what the following piece of code is supposed to do?
--code--
.replace(/^d/,'_$&')
--------
scipio # 16. March 2005, 21:22
Rijk # 16. March 2005, 21:22
Here it is:
http://my.opera.com/quiris/journal/54 points to http://my.opera.com/forums/showthread.php?s=&postid=842398#post842333
"Above code generates CSS signuture for every entire site which lack such ID.
Rules:
- every dot in domain name is replaced with hyphen
- domain names started with count at beginning are started with underscore at beginning of css signature
- www. is cut off"
The part you asked about is the regular expression to replace a number with a underscore+number, because ID values are bot allowed to start with a number.
izzyo # 16. March 2005, 21:22
izzyo # 16. March 2005, 21:22
Friedrich # 16. March 2005, 21:22
Friedrich
YtseJam # 16. March 2005, 21:22
MarkSchenk # 16. March 2005, 21:22
MarkSchenk # 16. March 2005, 21:22
http://www.markschenk.com/webdesign/userjs/
Currently there are only two. The first is useful on W3C specs, and will make linking to specific areas in the spec useful. It will add links to the headers, so you just click on the header and the URL can be found in the addressbar.
The other is only useful for Dutch users, because it fixes a rendering error on www.nu.nl
Have fun!
Rijk # 16. March 2005, 21:22
Rijk # 16. March 2005, 21:22
The blank-page issue is something that needs to be fixed by the Opera developers. I've been told it is not as easy as it looks, but a fix would be appreciated by users!
boogs # 16. March 2005, 21:22
Did you consider supporting greasemonkey's existing distribution model with the metadata header and installing separate userscripts (http://greasemonkey.mozdev.org/authoring.html)?
It would be really cool if we could share scripts between the two browsers.
-- Aaron Boodman (the greasemonkey guy)
Rijk # 16. March 2005, 21:22
First of all: I'm not the developer
Currently Opera doesn't have the capability to store multiple scripts and tie them to specific sites, so interoperability is not yet feasable. That is why I hacked together my user.js framework script. From what I've seen, the Greasemonkey scripts that don't use XPath should be perfectly usable in Opera. Already I've added some of them to my example userscript (with attribution of course).
I'm sure our developers know about Greasemonkey. But I don't know much about our plans for future development in this area, and what little I know is probably NDA'd. But I'll ask the responsible developer about this.
nicomen # 16. March 2005, 21:22
yahoo.com or news.yahoo.com will now match story.news.yahoo.com
Friedrich # 16. March 2005, 21:22
jor # 16. March 2005, 21:22
Is it still available?
Rijk # 16. March 2005, 21:22
Rijk # 16. March 2005, 21:22
Friedrich # 16. March 2005, 21:22
So, would the following be possible: As long as the error message is displayed, initiate a reload every ten seconds (essentially a "retry till success" script)?
In any case, this means we can (kinda a hack, but it would work, wouldn't it? I don't really know Javascript.) customize our error messages, can't we?
Rijk # 16. March 2005, 21:22
non-troppo # 16. March 2005, 21:22
non-troppo # 16. March 2005, 21:22
1. Initialise a variable at the beginning of Rijk's script:
var notify='';
2. Add the following function to the function list:
function ScriptStatus() {
window.defaultStatus=notify;
}
3. Add the function as the last but 1 option on the allHandler list, like:
allHandler=[
bodyIDforAll,
killBlank,
ScriptStatus, //here it is
doNothing];
4. To each function add something like this as the last line
notify += '-Unique name-';
so for e.g.
// 'backgroundToGreen' can be used to test if the user script is working.
function backgroundToGreen(){
document.body.style.background = "green";
notify += '-Green-';
}
This will let you see which functions are activated for the page by providing the info in the status bar. Commenting out the line in allHandler can then turn messaging on/off
scipio # 16. March 2005, 21:22
scipio # 16. March 2005, 21:22
ILoveJD # 16. March 2005, 21:22
The problem with that, and the reason not many people caught on to it, was that not many web site developers added the tag to their sites.
This addition to Opera 8 should mean that you can set a script to run when you load a site to automatically give the body tag this ID. Then, it's fairly easy to (if you wanted to) define custom styles to apply to only that site, using the body id tag.
For example, Yahoo's interface is a bit of a mess. The user.js file could add 'id="www-yahoo-com"' to the body tag of all yahoo pages. Then in your user style sheet you could define styles just for yahoo, like so:
#www-yahoo.com * {
font-family: Georgia;
}