#exclude in userscript not working as expected - greasemonkey

I have a userscript that I would like to run for a particular domain, but not for a particular page.
For example, I want to stop the script from running when the URL contains documentMode=edit:
https://blah.itglue.com/2443511/docs/8093867#documentMode=edit&version=draft
I tried this:
// #exclude https://blah.itglue.com/*/docs/*#documentMode=edit
// #exclude https://blah.itglue.com/.*/docs/.*documentMode=edit&.*
// #match https://blah.itglue.com/*
However, the script still runs and I'm not sure what I'm doing wrong. I can see the URL added to exclude in Tampermonkey.
I checked this in a regex live editor and the above URL matches perfectly. I refreshed the page but the userscript is still applied....
However, if I use this, it stops the script loading on the page:
/(^[^:\/#\?]*:\/\/([^#\?\/]*\.)?blah\.itglue\.com(:[0-9]{1,5})?\/.*$)/
I don't want the script to stop working on the root domain though.....

Related

Get URL of last redirected address NodeJS

I'm trying to get the last redirected address of a LinkedIn address: https://www.linkedin.com/school/18451/?legacySchoolId=18451 which in the browser gives: https://www.linkedin.com/school/babson-college/
In NodeJS, I have tried the following (I have tried all the solutions of that post):
request({ url: 'https://www.linkedin.com/school/18451/?legacySchoolId=18451', followRedirect: false }, function (err, res, body) {
console.log(res.headers.location);
});
But I'm still getting the same initial address (legacySchoolId) instead of the final (babson) one. It seems that the redirect is performed by a javascript function so I was wondering how I could get the last address in all use cases.
I tested it and I see two obstacles here:
1) You get the final URL only when you are logged in, otherwise you get a JS redirect to an authwall.
2) The final URL that you see in the browser does not come from a redirect, instead the displayed URL is just rewritten using replaceState (HTML5 history API). There is no navigation to a new page there.
I see two options to solve this:
1) Use a headless browser like Puppeteer. Write code to log in with your username and password and then navigate to those URLs, wait a bit (for example until some company info gets rendered) and then read the current URL.
2) Simulate only the most necessary requests and extract the info from the page (not sure if it works with LinkedIn though), using a library such as slimtomato.* You'd then start by simulating a login with your username and password and then use the same tomato object (or at least the same cookie jar) for the requests to those school links in order to get the final URLs. I didn't find a straight-forward way to see the final URL in the page source, but what would still work in this specific case though is parsing the page for this meta tag...
<meta name="apple-itunes-app" content="app-id=288429040, affiliate-data=ct=campaign_vw_smart_app_banner&pt=10746, app-argument=voyager://school/babson-college/?trk=vw_smart_app_banner">
...and then using the app-argument value (voyager://school/babson-college/?trk=vw_smart_app_banner) without the query and replacing voyager:// by https://www.linkedin.com/.
*: Disclaimer: I wrote that library. But I didn't find a good alternative with the same scope.

codeigniter making duplicating pages with #! include in link of each page

I have an issue on my website which is on CodeIgniter.
All links are making a duplicate copy of each with #! included in URL
for example
https://www.example.com/port-of-portsmouth
link to a page on my website which is duplicated like the below link
https://www.example.com/port-of-portsmouth#!
Both links have the same page content. How to get rid of this error to avoid content duplication
How to get rid of this error to avoid content duplication
You can't
Because # is used as a fragment identifier. For an example, if the URL is https://example.com/#data this means that open the example.com and jump to a specific section of the page with id"data"
See this URL, https://jestjs.io/docs/en/cli.html#cache
When you open the page, it jumps to cache section.
So, basically, your content is not duplicated, it's just opening the same page and then browser tries to navigate to id !, which might not be there and hence nothing special happens.

My Greasemonkey script runs on every site except the one I want it to?

I'm trying to set up a script to run on Quora, but it's running on every other site instead.
I have the #include directive in the metadata block, and under script settings/include, it's set to include *, and I can't see any way to remove that.
Here's a thread about the same issue I'm having, but there's no #exclude directive to remove (although I did try putting #exclude * before the #include, but that didn't fix anything). I also checked my formatting against the answer here and I don't see any typos.
Here's my metadata block.
// ==UserScript==
// #name Quora fixer
// #namespace quora
// #include https://www.quora.com/*
// #version 1
// #grant none
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// #description prevents links from opening in a new tab
// #run-at document-idle
// ==UserScript==
First, see and follow My very simple Greasemonkey script is not running?.
If the script is running on every other page, and there are no Quora-specific #excludes, then the script most-likely is running on Quora -- just not doing what you expect.
Other issues or actions:
You are using #grant none and #require-ing jQuery. This leads to conflicts and javascript crashes on sites like Quora. Use #grant GM_addStyle.
If the Script Settings tab (not to be confused with the User Settings tab) shows differently than the actual script source, then either you are not looking at the same file Greasemonkey is, or Greasemonkey and/or Firefox has become corrupted/confused. In that case:
Uninstall the Greasemonkey script.
Completely clear the browser cache.
Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
Go to your Firefox profile folder.
Enter the gm_scripts folder therein.
If there are any subfolders left over from the script you just uninstalled, delete them. In your case, the folders should have names similar to Quora_fixer.
Restart Firefox.
Make sure the script is not listed.
Install the Greasemonkey script afresh.
If it still doesn't work, create a new Firefox profile or try a different computer altogether.
Once the script fires on Quora and no relevant errors show on Firefox's Browser Console (see the first link above), then if the script still does not do what you expect, you may need to apply AJAX-aware techniques.

ModX redirect based on query string (Revolution 2.3)

I am rebuilding a website in ModX and I want to redirect the old URLs to the new ModX pages, automatically.
An old URL is of the form, http://www.oldsite.com/?pg=2
Every page is like this, so I need to manually map the old page IDs to the new ModX resource IDs. For example, pg=2 is the contact page, which is now resource ID 11, so I'll end up with a map like [2=>11, 3=>15, etc]
If I tweak the main index.php right in the docroot, this does exactly what I want:
/* execute the request handler */
if (!MODX_API_MODE) {
if (isset($_GET["pg"])) {
if ($_GET["pg"] == 2) {
$url = $url = $modx->makeUrl(11);
$modx->sendRedirect($url);
}
else {
// Page is set, but we don't have a redirect for it.
$modx->handleRequest();
}
}
else {
$modx->handleRequest();
}
}
However, I am not happy with hacking index.php directly. I'm a bit short of ModX experience to know exactly where to put this code. I tried:
A snippet, which I then called from my HTML header before any HTML, but the redirect stopped working
The Redirector extra, but this doesn't work on the QUERY_STRING, I don't think
Any insight is appreciated, for the best place to package this code, or a pointer towards an Extra I should be using.
The solution that worked for me, following Sean's insights below, is a plugin. The plugin code is below. For other plugin newbies like me, ensure you visit the "System Events" tab to enable your plugin for the event you're trying to access.
<?php
if ($modx->event->name == 'OnWebPageInit') {
// Look to see if the GET params include a pg. If they do, we have a request
// for one of the old pages.
if (isset($_GET["pg"])) {
// Map the old pg IDs to the new resource IDs.
if ($_GET["pg"] == 2) {
$url = $modx->makeUrl(11);
}
// Add more here...
// When done trying to match, redirect.
// But only do the redirect if we found a URL.
if (isset($url)) {
$modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
exit;
}
}
}
My preference to do this is in the .htaccess file with redirects or url rewriting - that way you can send the redirect and the response code ~before~ modx has to process anything [save a bit of overhead]
if you still want to do this in modx, take a peek at the sendRedirect docs & send the correct response code [so google gets the hint that the page has actually moved] Note: the $responseCode option is depreciated and you should use it in the options array these days:
$modx->sendRedirect('http://modx.com',array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
I do agree with not hacking the index.php file, only will cause you grief. What you want to do is place your redirect code in a plugin. Check the Modx API docs for the appropriate event for it to fire on - perhaps: OnWebPageInit will do the trick. Sorry, I don't know exactly which one will work.
HOWEVER ~ IMPORTANT NOTE!
Not all events are actually active, they may show up in the modx manager but don't actually do anything, you will just have to test or dig through the code to find out. [or ask in the community] Again, sorry, I don;t know for sure which ones work and which don't.

Greasemonkey #exclude does nothing

Greasemonkey script excludes do nothing, for some reason. They do not even seem to be recognized by Greasemonkey, and I would like to know why.
I have to go into the preferences for each script and add the excludes manually if I want the desired behavior. Apparently I'm getting the syntax correct, since a simple script like (there's nothing special about my choice of domain names, and I've checked it with others):
// ==UserScript==
// #name Delete Adsense Junk
// #exclude http://en.wikibooks.org/*
// #exclude https://en.wikibooks.org/*
// #include http://weather.org/*
// #include https://weather.org/*
// #version 1
// ==/UserScript==
alert("Alert!");
tells me all about where the script will and won't execute when I go to install it. Firefox wants to run the script on every page unless I navigate to the preferences for that script and change the user excludes though. The list of script excludes is empty and the include is just the wildcard. Is there some option that should be enabled/disabled that I'm missing? The user excludes seem to work normally. I'm just trying to figure out why Greasemonkey is ignoring the directives that I put in the script.
The script runs on the page and on all its frames. The alert box tells you which domain it is from (at least in FF). For example on YouTube it also runs for https://plus.google.com and https://s.ytimg.com. So for YouTube I get a total of 3 alerts and if youtube.com is excluded still 2 alerts.
Check out this post to see how to exclude them.

Resources