i have this code:
==UserScript==
// #name name
// #namespace url
// #description desc
// #include http://www.facebook.com/*
// #require http://code.jquery.com/jquery-1.5.2.min.js
// ==/UserScript==
$(document).ready(function() {
$("a").click(function(){
alert(1);
return false;
});
});
but when i install it, and click on some link i'm just taken to the adress, which means the script doesn't work.
can anyone tell me where's my mistake?
Your code looks fine and works for me on FF 4.0.1 with GM 0.9.2. You haven't specified the Firefox or GM version that you're working with - but I think I remember running into a problem where the external scripts would not get loaded. You might try copying and pasting the entire minified source of jQuery into your GM userscript, just after the header, before your code.
You might also want to make sure your code actually does get loaded by putting in some log/alert statements - I'd suggest $().jquery at least to ensure you have jQuery loaded and check it's version.
Related
I have a super simple script (running in a Userscript manager for Safari) that works on some pages but not all.
Here is the script:
// ==UserScript==
// #name Close tab on double click
// #include https://*
// ==/UserScript==
document.addEventListener("dblclick",function (event) {
window.close();
});
For some reason, it works on Stack Overflow, Reddit post pages, etc., but does NOT work on Reddit homepage, Google (homepage or search results), etc.
Why?
Thanks for any insight. I'm still a newbie about javascript.
Edit: Working now, using the GM.closeTab permission from my Userscript manager, as documented here: https://github.com/quoid/userscripts#api
Try using
// #grant window.close
in order to use window.close per the documentation.
In a chrome browser, when I add the JS function in the console and then double click on the page I get this notification (I tried the stack overflow site):
Console Image
My guess is your userscript manager may add the script you write differently depending on the website, and the websites that don't work are because of this warning. Try opening your console after you add the script and see if you get this warning when double clicking does not work.
I'm running a simple Greasemonkey/Tampermonkey user scrip in Firefox which gets selection of a text (let's say, just a single word) and opens a translator for it.
// ==UserScript==
// #name translator
// #version 1.0
// ==/UserScript==
document.addEventListener('dblclick', handleDblClick, true);
function handleDblClick(e) {
var txt = window.getSelection().toString();
window.open("https://translate.google.ru/?hl=ru&text=" + txt);
}
This works fine with ordinary web-pages, but not with built-in PDF Viewer. Is there any chance to make this (or any other) script running while viewing PDF-files in Firefox?
Here is a small sample of PDF-file to try with: http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf
I have a similiar problem and came to the conclusion that your desired behaviour is unfortunately not possible after Firefox 60. However, bookmarklets work in the built-in PDF viewer. One possible fix would be to rewrite your userscript to work as a bookmarklet.
Here is a bug report indicating that the current behaviour is intentional: https://bugzilla.mozilla.org/show_bug.cgi?id=1454760
Good luck!
I am writing a plug-in for Firefox and using greasemonkey script to do that (I compile the user script using this tool http://arantius.com/misc/greasemonkey/script-compiler).
The issue is that the script is run after the page is fully loaded. Meaning the user would see the viewed page in its original form and then the script will apply the changes that I made. My question is there a way to run the user script before the contents of the page is displayed to the user, so the user would only the final version of the website?
EDIT: This post was created prior to the implementation of the #run-at key in Greasemonkey - as noted by Blaise, from Greasemonkey 0.9.8 you may now have the script executed as soon as the page begins to load.
#run-at document-start is described in the wiki as follows:
Start is new as of version 0.9.8. The script will run before any
document begins loading, thus before any scripts run or images load.
Note that this means having the script run (potentially) prior to the creation of the DOM and may lead to some inconsistent/unusual behaviour. You will need (a little) more than just this one-line drop-in.
There are a few more details over at the Greasemonkey Wiki:
http://wiki.greasespot.net/Metadata_Block#.40run-at
Side-note: The information I have to hand is specifically related to Greasemonkey in the browser. I am unsure whether #run-at document-start works well with the script compiler - I suggest living dangerously.. Test it and find out! ;]
It is not currently possible to run a user script before the page loads.
To stop the flicker using current versions of Greasemonkey, you could try adding a user-style to your Firefox profile (which is then undone using the script) as described at the Greasemonkey wiki but this would also require each of your users to do the same to benefit from this.
It is something that has been desired for a long time and looking through issue #1103 on Greasemonkey's Github site, a working prototype appears to have been made (but there is no timescale for this to be added to a release version afaik).
In addition to the previous answers I've found a solution that works flawlessly, just like previous versions of Firefox & GreaseMonkey.
First off, run-at must be set to the earliest moment, so document-start:
// #run-at document-start
Because the DOM is probably not loaded yet, wait for the document to get a readyState of interactive:
document.onreadystatechange = function () {
if (document.readyState === "interactive") {
// Do something
}
}
While Greasemonkey implements more or less the same with document-end, I've noticed that the technique above works faster.
Using this has resolved all issues I had in Firefox with flashing / jumping screens on pages when the DOM changes kicked in and directly loads the page as intended with the userscript.
Using the #run-at document-start as suggested in the answer by #kwah my script executed before the content I wanted to manipulate was in the document body. As a work-around I set up an interval running my script 5 times/second until document.readyState == "complete" (or until 20 seconds have passed).
This worked well for my case:
// ==UserScript==
// ...
// #run-at document-start
// ==/UserScript==
var greasemonkeyInterval = setInterval(greasemonkey, 200);
var greasemonkeyStart = (new Date()).getTime();
function greasemonkey() {
// ...
// waiting for readyState (or timeout)
if (
(new Date()).getTime() - greasemonkeyStart > 20000
|| document.readyState == "complete"
) {
clearInterval(greasemonkeyInterval);
}
};
If you're more sure about that the content will be there at documentready I think something like this would be more preferable:
function greasemonkey() {
if (
document.readyState != "interactive"
&& document.readyState != "complete"
) {
return;
}
// ...
clearInterval(greasemonkeyInterval);
}
Is there a way to execute some javascript on load a web site.
For example I want to hide facebook chat and I want to execute
document.getElementById('pagelet_chat_home').style.display = "none" on loading a site.
You could wrap the needed actions into a greasmonkey script - Chrome supports greasemonkey user scripts out of the box and converts them into a plugins automatically.
Save the following as script.user.js and open it with Chrome. Chrome should detect it as a plugin and install it. Didn't check if it actually works but there shouldn't be any problems.
// ==UserScript==
// #name Hide Pagelet
// #namespace hide_pagelet
// #description An example Greasemonkey script that hides a pagelet
// #include http://*.facebook.com/*
// ==/UserScript==
document.getElementById('pagelet_chat_home').style.display = "none";
I have created a Greasemonkey script that runs fine in the firebug editor, with the Greasemonkey specifics removed, but not when I try to package it as a userscript. The Firefox error console is reporting that an iframe I am trying to use is undefined.
I've cut the userscript down to a minimum case where it should be printing the iframe html into the firebug console, and does when run in the firebug editor, but is not working as a userscript:
// ==UserScript==
// #name Movies
// #include http://*.princecharlescinema.com/*
// #include http://princecharlescinema.com/*
// ==/UserScript==
// stop script loading multiple times
if (top !=self) return;
var iframeHTML = window.frames['iframe2'].document.documentElement.innerHTML;
unsafeWindow.console.log(iframeHTML);
An example page the script is intended for
If it's of any use the gist of the full script is I collect all the td tags from this iframe, get some information from them, and then insert some new html into some of the same td tags.
Any help would be appreciated.
Perhaps you need to wait for DOMFrameContentLoaded