Automatically edit url of page - javascript

At our school website, the homework pages for our teachers only shows the upcoming assignments.
To show all of the assignments you click See All and then All Homework.
The URL changes in the following way
http://example.com/apps/classes/show_class.jsp?classREC_ID=000000
http://example.com/apps/classes/show_assignment.jsp?classREC_ID=000000&showAll=true
I have looked (mainly on Stack Overflow) for solutions such as User Scripts that will automatically change the URL, but I have not been able to figure out how to do this based off of other peoples questions/answers.
To keep it short, how would I be able to modify the URL so show_class becomes show_assignment and has &showAll=true appended to it at the end.
Thanks for any help.

This would do it:
window.location = String(window.location).replace(/show_class/,"show_assignment") + "&showAll=true";

Are you trying to modify links, or modify buttons, or redirect all show_class.jsp pages?
If you are trying to modify links, buttons, etc. Edit your answer to show the appropriate HTML source of the target page.
If you want to redirect, a script like the following should do it:
// ==UserScript==
// #name _Redirect to see all homework assignments
// #include http://example.com/apps/classes/show_class.jsp?classREC_ID=*
// #run-at document-start
// ==/UserScript==
var newURL = location.href.replace (/show_class\.jsp/i, "show_assignment.jsp")
+ "&showAll=true"
;
//--- Keep browser history / back-button, uncluttered.
location.replace (newURL);

Related

Way to Automate Pressing of Specific Button on Page Load?

I'm trying to figure out a way to simply click on a specific button (which is an href) on a particular page when it loads, via a userscript written in Javascript within Tampermonkey. It seems straightforward enough, but I haven't gotten any of my code to work, after multiple iterations.
Here is the full source code of the href and button that I want pressed:
<span data-react-class="require('reactComponents/workPipeline/SubmitAcceptTaskForm')['SubmitAcceptTaskFormLink']" data-react-props="{"text":"accept","refTag":"w_wp_acpt_warning"}"><span data-reactid=".4">accept</span></span>
Here is the code that I have been trying to use (I have tested each individual line separately, and to no avail):
// ==UserScript==
// #name Accept Button WIP
// #include https://worker.mturk.com/projects/*
// ==/UserScript==
location.href = document.querySelector("a[href='#']");
document.querySelector("a[data-reactid='.4.0']").click();
document.getElementsByClassName(".4.0")[0].click();
location.href = document.querySelector("a[href='#']");
location.data-reactid = document.querySelector("a[data-reactid='.4.0']").click();
document.querySelector("span[data-reactid='.4']").click();
And here is a screenshot of everything, including the DIVs that the button is contained within (the mouse is hovered over the necessary Accept button):
I'm not too sure with what I'm doing wrong. I thought one of the lines of code that I tried would have worked, but there is probably something I am overlooking in terms of syntax. Any and all help would be greatly appreciated. Have a wonderful day & night.
Hard to tell why it isn't working for you... For me it worked without problems with the following HTML test on localhost:
<html>
<head>
<script type='text/javascript'>
function check(){
console.log('Hellow world');
}
</script>
</head>
<body>
<span data-react-class="require('reactComponents/workPipeline/SubmitAcceptTaskForm')['SubmitAcceptTaskFormLink']" data-react-props="{"text":"accept","refTag":"w_wp_acpt_warning"}"><span data-reactid=".4"><a href="#" data-reactid=".4.0" onclick='check()'>accept</a></span></span>
</body>
</html>
And the following userscript:
// ==UserScript==
// #name Autoclick
// #namespace http://tampermonkey.net/
// #version 0.1
// #match http://localhost/test.html
// #grant none
// #run-at document-end
// ==/UserScript==
(function() {
'use strict';
document.querySelector("a[data-reactid='.4.0']").click();
})();
For me, it worked without the autoexecutable function and I just added the #run-at directive for good measure but it works without it.
The "Hello World" message appears on the console after page loading.
One tip is that most of the times, the code that you want to run on the userscript should run exactly the same from the console. This way, you can test live what works or not.
I decided to go a different route and select a different button. For whatever reason I couldn't get the code to activate on page load, but opted to select the other Accept button on the page via:
document.getElementsByClassName("btn btn-primary")[0].click();

Add to favorites button for adding favorite html Pages

I am creating a mobile website with different events in a specific month. I want to add a button on every event page let user add different as'favorites'. I want that when user click the Add to favorites button, the event should be automatically added to a html page 'favorites'
I would go with a small javascript, external and called on the event pages (since there will be many instances of it). It's actually a very simple process you're asking about.
Create a new javascript file called favorites.js
Save it with this code:
Modified To support Opera
function bookmark(title,url){
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
url =url+sPage;
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}
In the head of every HTML document you write, you need to call this script (extremely similar to calling a CSS definition):
<script src="**/dir/**favorites.js" charset="UTF-8" type="text/javascript" defer/></script>
Defer is optional but recommended with a script like this which is not essential to the functioning of the page. However, if you want the possibility that the user can make use of the add to bookmarks function before the rest of the page loads, you may change defer to async.
Now you're almost there. At this point, to save yourself some frustration, make sure that your pages load without error before trying to use a link. This will ensure there are no bugs with your javascript or existing code.
To make use of the add to favorites button, it's as simple as using anchors and links. A link which would make use of this script in particular looks like this:
bookmark me
or
<img src="imgs/bookmarklogo.png" **other-img-definition-stuff**>
And in the old days it was common to use a form button, like this:
<input type="button" name="Bookmark this page" onclick="bookmark()" />
Several years have gone by since I really bothered myself with HTML/CSS, and in that time, options such as AddThis have come about. If you find this task too difficult, I would just go with something like that. And also I would recommend getting away from handcoding and go to some sort of CMS, like Drupal or Wordpress. Especially if you are on tight schedules.

How to make Greasemonkey click automatically on a "weird" link on a specific site?

I'm not really a coder; I can hack some scripts but no more. Talk to me like a REAL NEWBIE. ;-)
I want to automatically expand two links on a page, using a GM Script.
It's a French dating site and, in the profile section, you have two places where the default is to show only partial information. So, to have all the information, you need to click on 2 additional links.
Here's a screenshot of the page with the links that I want to trigger:
(Click for larger image)
The target page is reseaucontact.com/profil/3604181
IMPORTANT: When I was testing the Script I was logged in the site... so if you click on the link & you are NOT logged-in, the site will tell you "Register to see full content" in French.
I tried to make a Script to open the first link but since the URL of the link is the SAME URL that the page (reseaucontact.com/profil/3604181) the page was just reloading in an infinite loop ;-)
Here's my First attempt to make a script the result was an infinite loop:
// ==UserScript==
// #name Reseau Contact Automatic Expander
// #version 1.0
// #author Mikha
// #include http://reseaucontact.com/profil/*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// #grant GM_addStyle
// ==/UserScript==
/*- The #grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//--- Note that the contains() text is case-sensitive.
var TargetLink = $("a:contains('Voir tous les détails')")
if (TargetLink.length)
window.location.href = TargetLink[0].href
Here's the data/print-screen from Firefox Web Developer tools I have collected (When I'm logged) to find the targets/links:
The first link:
(Click for larger image)
The second link:
(Click for larger image)
I understand that when I click on the 2 links it's a GET command for "authentication request" or something like that... but I don't understand how make a script that will really "mimic" the click of my mouse to make expand the 2 sections of the profile without clicking each time.
I'm pretty sure it is simple/easy to do but after 4 hours of trying to find the information with that "kind of links" I need helps.
If you need more information or clarification just ask me.
These are AJAX-driven links with dummy values in the href. Clicking the link fires off javascript events.
The easiest thing to do in this case is to send the mouse event(s) that the javascript expects (usually, but not always a click event). See Choosing and activating the right controls on an AJAX-driven site.
Also, as a practical matter, having a GM script immediately fire on two jQuery-AJAX driven links, like those two are, might run into timing or "race" problems. For sites like this, I recommend a slight delay before clicking. Use setTimeout()Doc for the delay.
Putting it all together, the complete script could would be like:
// ==UserScript==
// #name Reseau Contact Automatic Expander
// #version 1.0
// #author Mikha
// #include http://reseaucontact.com/profil/*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// #grant GM_addStyle
// ==/UserScript==
/*- The #grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//-- Delay the link clicks 333 mS and 777 mS, respectively.
setTimeout (clickLinkWithText, 333, "Voir tous les détails");
setTimeout (clickLinkWithText, 777, "Lire la suite");
function clickLinkWithText (linkText) {
var targetLink = $("a:contains('" + linkText + "')");
if (targetLink.length) {
triggerMouseEvent (targetLink[0], "click");
}
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
Adjust the delays if needed, and beware that there is a small chance that the site uses more, or different, events than click.
If the functionality of clicking on these truncated links is to expand the element or grab more content with an AJAX call or just show a hidden element below, then you don't probably need the link's HREF at all since its probably preventing the default behavior of following the href to a new URL.
Instead of messing with the href try triggering the click event on these links instead:
TargetLink.click();

How to change the Yahoo Mail, sign out URL?

This is my code I came up with (w/ lots of outside help; I'm not fluent in JS):
setTimeout(function() {
var logout = document.querySelector('a.yucs-signout'),
link = "http://login.yahoo.com/config/login?logout=1&.src=cdgm&.intl=us&.direct=2&.done=http://ma
il.yahoo.com";
logout.setAttribute ('onclick', 'location.href =
\"' + link + '\"');
logout.setAttribute ('href', link);
}, 17000)
I'm trying to change the sign-out URL at Yahoo Mail, when clicking the "Sign Out" dropdown menu item, so that you are redirected back to the Yahoo Mail login page -- not the yahoo.com "main page". This is to make it easier to login with another account.
We couldn't get it to work. Even added a timeout to the code in case my js was running too soon. Still no go.
I was told "The class="yucs-submenu-toggle" on the <a id="yucs-menu_link_profile"> with no CSS on :hover means javascript is being used."
Logout control screenshot:
You have to hover over that section to get the menu to dropdown & see Sign Out.
I also made sure my "Included page" was https: https://*.mail.yahoo.*/*
I'm trying to use this with Greasemonkey, why isn't it working?
Edit: I was thinking this other answer might have something useful, like the jQuery stuff in it?
If the code in the question is copied verbatim, then it has syntax errors from improperly made, multi-line strings.
Don't set onclick from a userscript. Ever! In this case, no form of click event handling is necessary anyway, and may block the logout operation of the page.
Other than that, that code works for me -- at least on Yahoo, UK, mail.
Some more minor points:
You don't need a setTimeout in this case.
Don't brute-force the link like that; it may not always work. Use regex to surgically change just the done variable part.
Here's a complete working script:
// ==UserScript==
// #name _Fix Yahoo mail logout
// #include http://mail.yahoo.com/*
// #include http://*.mail.yahoo.com/*
// #include https://mail.yahoo.com/*
// #include https://*.mail.yahoo.com/*
// ==/UserScript==
//-- Only run in the top page, not the various iframes.
if (window.self === window.top) {
var logout = document.querySelector ('a.yucs-signout');
if (logout) {
var link = logout.href.replace (
/&\.done=[^&#]+(&?)/, "&.done=http://mail.yahoo.com$1"
);
logout.href = link;
}
else
console.error ("From GM script: Node 'a.yucs-signout' not found.");
}

thickbox not behaving like it should

I'm creating a greasemonkey script. One of the functions it should do is close any thickboxes that open with the page (like when you visit sidereel for the first time). I put this line in
$('.ui-icon-closethick').click()
Yet it doesn't seem to do the trick. Am I missing something?
The script is being executed after the popup is opened, so basically you can't prevent it from opening using Greasemonkey.
However, there is an addon called Mason that will help you.
Just add the following rule:
Description : sidereel : Fake login
Include URL : ^http:\/\/www\.sidereel\.com\/
Function : Cookie Definition
Config... : seen_saved_source_popup=true
Leave the other fields blank.
If flashing the popup isn't the problem, then you may use the following:
// ==UserScript==
// #name sidereel : hide welcome
// #namespace http://stackoverflow.com/questions/5288097/
// #include http://www.sidereel.com/*
// ==/UserScript==
if (unsafeWindow.$)
unsafeWindow.$("div[id *= 'lightbox-saved-source-']").dialog("close");

Categories

Resources