Replacing an extension-less image in gmail HTML view with tampermonkey - tampermonkey

On HTML gmail view top left corner the new Gmail logo could be seen. I want to replace it with the old one ( https://i.imgur.com/kAQfeoj.png ) but I'm not sure how. I've tried a 9 year old stackoverflow thread but it didn't work since the gmail logo link isn't a link to the file with an extension.

found a way
// ==UserScript==
// #name Image Replacer
// #version 1.1
// #description Image Replacer
// #require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// #match https://www.pinterest.com/pin/*
// #grant none
// ==/UserScript==
/*globals $*/
var old_url = "https://ci6.googleusercontent.com/proxy/WQ_JO_iFWK3AKuIPeHz_fnMMOaGLG0HAsCJWOkWiRo9Oa_NHUeQYCdmJEfw8llIVJ8fMP1mriJ81iPzrebHtesFsF7VIDS0QKCFmqu_RMT_-ow=s0-d-e1-ft"
var new_url = "https://i.imgur.com/kAQfeoj.png"
$(document).ready(function(){
$("img[src='"+old_url+"']").attr("src", new_url);
});

Related

Why does a tampermonkey script for https://news.google.com/* appear in the menu for https://www.nytimes.com/?

I have a tampermonkey script with this header:
// ==UserScript==
// #name Google News - Remove old articles
// #namespace http://tampermonkey.net/
// #version 0.2
// #description Remove articles that already appeared recently
// #author AC
// #require http://code.jquery.com/jquery-latest.js
// #match https://news.google.com/*
// #icon https://www.google.com/s2/favicons?domain=google.com
// #grant none
// ==/UserScript==
When I am on the page https://www.nytimes.com/ and I click on the tampermonkey icon in the Firefox toolbar, this script appears. Why?
Because the page contains an iframe, and that iframe links to news.google.com:
Userscripts run in iframes, and when they do, they get added to the Tampermonkey icon at the top.

removing an element with a userscript

First attempt at a userscript, and despite finding numerous examples on the internet of how to remove elements from a page, none of them work (and this looks like one of the most basic applications of a userscript, too).
Using violentmonkey-2.12.8 on this page:
https://www.zerohedge.com/economics/2020-greatest-hits-most-popular-articles-past-year-and-look-ahead
I want to remove the "exitModalOverlay" div (disabling it in the developer tools does exactly what I want), which blacks out the page (preventing me from reading it).
I will insert one of the more common techniques I have found (which doesn't work). I would appreciate any method which does. Thanks.
// ==UserScript==
// #namespace confused
// #name zehohedge_remove_subscription_popup
// #version 1
// #description removes the overlay div that asks to subscribe
// #match https://www.zerohedge.com/*
// #grant none
// #noframes
// ==/UserScript==
var todelete = document.getElementById('exitModalOverlay');
if (todelete) { todelete.parentNode.removeChild(todelete); }
Based on the post & comments, it seems the element is loaded/created after the DOM. In that case, you would need to run the script after page loading is complete.
Although when testing on the link provided with JavaScript enabled (https://www.zerohedge.com/economics/2020-greatest-hits-most-popular-articles-past-year-and-look-ahead), the element does not appear, here is an example of how you can remove the item.
It is possible there are other factors involved (e.g. browser, country, login, cookies etc).
ViolentMonkey by default runs on document-end which is after DOM loaded but before all external elements are loaded.
Setting the userscript to run at document-idle will run after everything is loaded.
// ==UserScript==
// #namespace confused
// #name zehohedge_remove_subscription_popup
// #version 1
// #description removes the overlay div that asks to subscribe
// #match https://www.zerohedge.com/*
// #grant none
// #noframes
// #run-at document-idle
// ==/UserScript==
// find element
const todelete = document.getElementById('exitModalOverlay');
// remove element if found
if (todelete) { todelete.remove(); }
Removing the element is not the only way to get rid of an element. You can also use CSS to achieve a similar outcome by setting its display to none. For example:
// ==UserScript==
// #namespace confused
// #name zehohedge_remove_subscription_popup
// #version 1
// #description removes the overlay div that asks to subscribe
// #match https://www.zerohedge.com/*
// #grant GM_addStyle
// #noframes
// ==/UserScript==
const css = `
#exitModalOverlay {
display: none;
}`;
GM_addStyle(css);
Using JavaScript to apply CSS to the element without GM_addStyle (although not as good as above)
// ==UserScript==
// #namespace confused
// #name zehohedge_remove_subscription_popup
// #version 1
// #description removes the overlay div that asks to subscribe
// #match https://www.zerohedge.com/*
// #grant none
// #noframes
// #run-at document-idle
// ==/UserScript==
// find element
const todelete = document.getElementById('exitModalOverlay');
// remove element if found
if (todelete) { todelete.style.display = 'none'; }
📌 It is worth noting that CSS applies all the time (unless over-written specifically), even if an element is created later, while JavaScript applies at the time it runs and will not apply to elements created later (without additional methods to cause it to run again).
Maybe so...
window.onload = function() {
var todelete = document.querySelector('#exitModalOverlay');
todelete.outerHTML = '';
}
or
window.onload = function() {
var todelete = document.querySelector('#exitModalOverlay');
todelete.remove();
}

How Do I Make A Tampermonkey Userscript Replace Words In Text Files?

6 years ago I asked a question about rewriting text files displayed in a browser using greasemonkey.
Can I Make Greasmonkey Scripts Run On Text Files?
I am now coming back to a similar problem and I tried to paste it in to Tampermonkey but it doesn't replace the text.
What am I doing wrong here?
// ==UserScript==
// #name Rewrite LLVM License
// #namespace http://tampermonkey.net/
// #version 0.1
// #match http://llvm.org/releases/2.8/*
// #include http://llvm.org/releases/2.8/LICENSE.TXT
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replace("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
It looks like the page you're interested in redirects to:
https://releases.llvm.org/2.8/LICENSE.TXT
so that's what you need to set your #include or #match to.
You also want to replace all instances of LLVM, so use .replaceAll:
// ==UserScript==
// #name Rewrite LLVM License
// #include https://releases.llvm.org/2.8/LICENSE.TXT
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replaceAll("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
If you don't want to rely on replaceAll, use a regular expression with .replace instead: /LLVM/g.
It seems quite strange to rely on jQuery for something this trivial though - you can very easily accomplish this without a library:
// ==UserScript==
// #name Rewrite LLVM License
// #include https://releases.llvm.org/2.8/LICENSE.TXT
// #grant none
// ==/UserScript==
const pre = document.querySelector('body > pre');
pre.textContent = pre.textContent.replaceAll('LLVM', "Ernst Blofeld");

Userscript to bypass "You are now leaving" page

A web site's links external links all redirect to a "You are now leaving" page.
When I copy the originally posted link-URL, they look like this:
https://www.flashback.org/leave.php?u=http%3A%2F%2Fwww.swatab.com%2F%3Flang%3Den
Page HTML:
<a href="/leave.php?u=http%3A%2F%2Fwww.swatab.com%2F%3Flang%3Den"
target="_blank">http://www.swatab.com/?lang=en</a>
I would like to create a userscript that bypasses the /leave.php page and redirects me to the true link on click.
Is there a simple solution to this?
That's a job for a relatively standard redirect script.
Note the use of #run-at document-start and of location.replaceDoc.
// ==UserScript==
// #name Flashback.org, skip the "You are now leaving" page
// #match *://www.flashback.org/leave.php*
// #grant none
// #run-at document-start
// ==/UserScript==
var targUrlEnc = location.search.replace (/\?u=/i, "");
if (targUrlEnc) {
var targUrl = decodeURIComponent (targUrlEnc);
location.replace (targUrl);
}

How load a local CSS file to specific site

I'm looking for a way to make the function GM_addStyle add CSS straight from the #resource on my local drive (I use macOS). My goal is to speed up the development of this CSS file.
Right now I'm copy&pasting changes to Tampermonkey > saving the userstyle & reloading the page.
Not working:
// ==UserScript==
// #name my style
// #version 0.1
// #description a few friendliness tweaks
// #author thomas
// #match http://stackoverflow.com/*
// #resource my_style file:///Users/thomas/style.css
// #grant GM_addStyle
// ==/UserScript==
(function() { 'use strict'; GM_addStyle(GM_getResourceText('my_style')); })();
Thanks

Resources