I have this script which closes a tab if it finds a certain element on the page:
// ==UserScript==
// #name Name
// #namespace bbb
// #match https://example.com
// #include about:config
// #version 1
// #grant none
// ==/UserScript==
var intv = setInterval(function() {
if (/(container)|(mediumheight)/i.test (document.body.innerHTML) )
{
window.close()
}
}, 1000);
How can I make it close the page if it finds this:
<span id="some">0</span>
But it has to check for both the id (some) and for the number (0). Both must be on the page for the script to fire.
If it was just the id (some) I could do it, but I need both to be true for the script to fire.
Can anyone help? Thanks
<span id="some">0</span>
But it has to check for both the id (some) and for the number (0). Both must be on the page for the script to fire.
Here is an example of how you get check it:
const sp = document.querySelector('#some'); // find id (some)
if (sp && sp.textContent === '0') {
// run the function
}
Related
I'm a beginner is learning how to code for Tampermonkey.
I know how to click an automated clicking using a class tag or id tag.
But is there anywhere to automate a clicking based on data info such as data-id, name or image URL?
The HTML is like:
<div class="yellow-bot" data-image="//imgurl1.gif" data-id="123" data-name="Image1">...</div>
<div class="yellow-bot" data-image="//imgurl2.gif" data-id="124" data-name="Image2">...</div>
<div class="yellow-bot" data-image="//imgurl3.gif" data-id="125" data-name="Image3">...</div>
...
...
...
<div class="submitButton">
<input id="button" type="submit" value="Submit Now" class="btn-primary">
</div>
So I'd like the click to click id 124 and 125 but the class is all the same. and then click on the Submit button. Can anyone help me with this?
Reference CSS selectors and jQuery selectors.
So, selecting by attribute:
document.querySelector (".yellow-bot[data-id='124']").click ();
document.querySelector (".yellow-bot[data-id='125']").click ();
Or much more robustly:
// ==UserScript==
// #name _Click nodes by attribute
// #match *://YOUR_SERVER.COM/YOUR_PATH/*
// #require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// #grant GM.getValue
// ==/UserScript==
//- The #grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */
waitForKeyElements (".yellow-bot[data-id='124']", clickNode, true);
waitForKeyElements (".yellow-bot[data-id='125']", clickNode, true);
function clickNode (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
For the rest, see Choosing and activating the right controls on an AJAX-driven site.
Newbie question:
I'd like to scan a page for all links where the href starts with hide? and then I'd like to add style="float:left" to the link like:
<a href="hide?6765765" style="float:left">
How do I do this?
Thank you!
Here's one way. Google any unfamiliar terms (and also "jQuery selectors").
// ==UserScript==
// #name _Float "hide" links
// #match *://YOUR_SERVER.COM/YOUR_PATH/*
// #require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// #grant GM.getValue
// ==/UserScript==
//- The #grant directives are needed to restore the proper sandbox.
waitForKeyElements ("a[href^='hide?']", floatNodeLeft);
function floatNodeLeft (jNode) {
jNode.css ("float", "left");
}
Since IMDB search can't exclude specific genres, I would like to hide the ones who I am not interested in, using Tampermonkey or Greasemonkey.
Every movie is inside a class named "lister-item mode-advanced":
Inside that is:
<span class="genre">
Animation, Adventure, Family </span>
Looking at other answers, I thought something like this could work:
// ==UserScript==
// #name NoAnimation
// #namespace NoAnimation
// #version 0.1
// #description try to take over the world!
// #author You
// #include *.imdb.com/search*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// #grant GM_log
// #grant GM_getValue
// #grant GM_setValue
// #grant GM_addStyle
// #grant GM_openInTab
// #grant GM_xmlhttpRequest
// #grant GM_registerMenuCommand
// ==/UserScript==
$("lister-item mode-advanced") .show ()
.has ("span.genre:contains('Animation')")
.hide ();
which doesn't work of course :(
I'm doing this test on akas.imdb.com/search/title?....
I hope I was clear enough. Can anyone give me advice? :)
The main mistake is, that is not how to specify class in a jQuery selector. It should have been $(".lister-item.mode-advanced").
But there are other problems:
Brittle selectors used. For example mode-advanced is not always present. May not always be a span, etc.
Unnecessary logic (.show(), for example).
Very obsolete version of jQuery.
Extraneous and unhelpful meta info.
Here's a complete script that addresses these issues:
// ==UserScript==
// #name Hide Animations from IMBD search results
// #match *://*.imdb.com/search*
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant GM_addStyle
// ==/UserScript==
//- The #grant directive is needed to restore the proper sandbox.
$(".lister-item").has (".genre:contains('Animation')").hide ();
This question already has an answer here:
How do I get Greasemonkey to click on a button that only appears after a delay?
(1 answer)
Closed 8 years ago.
Ok Here is the source of the page.
<div id="socialBox"></div>
<div class="friendButton addFriend">
<a>+friend</a>
</div>
Here is my greasemonkey code
// ==UserScript==
// #name Auto click
// #namespace Auto click
// #description Auto click
// #include https://*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// ==/UserScript==
waitForKeyElements ("#friendButton addFriend", triggerMostButtons);
function triggerMostButtons (jNode) {
triggerMouseEvent (jNode[0], "mouseover");
triggerMouseEvent (jNode[0], "mousedown");
triggerMouseEvent (jNode[0], "click");
triggerMouseEvent (jNode[0], "mouseup");
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
greasemonkey says its executing the code so it clearly doesn't work. Let me know if you need more info
I wonder if the code below is what you want.
document.querySelector('.friendButton.addFriend a').click()
Demo
You'd better provide a link to one of the pages on which you want your script executed, so that I can make sure the script will work as expected.
I have done a few searches for this issue and I have come up empty handed. I hope somebody can clarify things for me and point me in the right direction.
Problem: I have a page that displays a list of results after submitting a search form. When a user clicks on one of the results, the browser goes to a new page showing more information about the result. When the user then clicks the 'back' button to go pack to the results, my browser reloads the page and shows the top of the page instead of the result that was last clicked.
Goal: What I would like is this: when the user click's the back button, the browser should go back to the previous page and, instead of showing the top of the page, show the page at the previous position.
Solution: I am completely lost as how this result can be achieved. Could it have something to do with javascript, or headers sent to the browsers, maybe something to do with caching.
If this is incredibly important, I'd suggest investigating the following:
add ids to each outgoing link
use JavaScript to capture the onClick for the links
when a link is clicked, redirect the user to that link's id fragment identifier, then link out as desired
When the user hits the back button, they'll return to that specific link, e.g. http://www.example.com/#link27 instead of http://www.example.com/
You may be able to get some ideas from here:
Stack Overflow:
Is it possible to persist (without reloading) AJAX page state across BACK button clicks?
YUI Browser History Manager
Ajax Patterns: Unique URLs
You can use javascript and jquery to set the scroll position of the window and cookies to store the position to scroll to. In the javascript of the page with the search results you could have something like this:
var COOKIE_NAME = "scrollPosition";
$(document).ready( function() {
// Check to see if the user already has the cookie set to scroll
var scrollPosition = getCookie(COOKIE_NAME);
if (scrollPosition.length > 0) {
// Scroll to the position of the last link clicked
window.scrollTo(0, parseInt(scrollPosition, 10));
}
// Attach an overriding click event for each link that has a class named "resultLink" so the
// saveScrollPosition function can be called before the user is redirected.
$("a.resultLink").each( function() {
$(this).click( function() {
saveScrollPosition($(this));
});
});
});
// Get the offset (height from top of page) of the link element
// and save it in a cookie.
function saveScrollPosition(link) {
var linkTop = link.offset().top;
setCookie(COOKIE_NAME, linkTop, 1);
}
// Boring cookie helper function
function getCookie(name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(name + "=");
if (c_start != -1) {
c_start = c_start + name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end ==- 1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
// Another boring cookie helper function
function setCookie(name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = name + "=" + escape(value) +
((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}
This assumes your search result links have class="resultLink".
The first part of the answer is that you use anchors to land on a page somewhere other than the top. So if I have this in my html at the bottom of my page:
<a name="f"></a>
then I can have the user land there by appending the anchor to the end of he url:
http://www.bullionvalues.com/glossary.aspx#f
So, if you are talking about ASP.Net you can place the anchor in a hidden field on the page info page and then read it from the search page by using: Page.PreviousPage property.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
Object o = PreviousPage.FindControl("hfAnchor");
if (o != null)
{
HiddenField hf = o as HiddenField;
Response.Redirect(Request.Url+"#"+hf.Value);
}
}
}
I fixed this issue by sending headers with php. This was my solution:
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: store, cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);
Thanks to everybody for the help.