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";
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 trying to start an embedded videojs video using Greasemonkey. The video is running in a iframe and I managed to start the video in chrome with Tampermonkey but in Firefox the video starts loading and stops a sec later. I guess my following script starts too soon after reloading the page so I tried to delay the start with setTimeout but the code won't start. So my question is it possible to use setTimeout inside an iframe?
// ==UserScript==
// #name Autoplay
// #namespace openload
// #include https://openload.co/embed/*
// #version 1.0.0
// #run-at document-idle
// ==/UserScript==
window.setTimeout(play, 5000);
function play()
{
console.log("Start");
document.querySelector('#videooverlay').click();
videojs.getPlayers()['olvideo'].player_.play();
console.log("End");
}
I use #include https://openload.co/embed/* to run the script inside the iframe otherwise I can't use the command .play().
If I open the iframeurl manually and use the commands over the FF console it works perfectly.
Maybe try something like this
window.addEventListener ("load", playerstart, false);
function playerstart () {
}
I think you would want to add an "onload" event listener to the page loading in the iframe, and in the load function add the setTimeout.
I think the bigger issue to to figure out why it's not playing in Firefox. Is it an encoding issue? Do you have it in a source that FF can play? Miro Video Converter can help.
Do you tryed to start the script on the parent and inside the iframe? Then use setTimeout.
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.
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