How to enforce traditional behaviour on clicking Links?

General discussion and chat (archived)
User avatar
ketmar
Lunatic
Lunatic
Posts: 365
Joined: 2015-07-28, 11:10
Location: Earth

Re: How to enforce traditional behaviour on clicking Links?

Post by ketmar » 2015-11-03, 07:08

DjogaRo wrote:Since my include mask spells "www.youtube.com/?*" I thought the asterisk would cover everything url-valid character. Plus I always started out on page "https://www.youtube.com/".
asterisk is, but "?" is not a special char in "include" masks. it should match literally, so matching fails.
DjogaRo wrote:So there is no case, that maybe the script works fine on google, but causes something like an exception on youtube retroactively undoing the console.log()?
as your script is executing at "document-start", there's nothing page can do to stop it — no page content is loaded yet. ;-) so you should see your test message if script is really loaded.

also, you can turn on GS debug logs (both debug and cache debug), then GS will report in Error Console which scripts were applied for the page.

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-03, 09:22

ketmar wrote:asterisk is, but "?" is not a special char in "include" masks. it should match literally, so matching fails.
That's priceless information there - would have never occurred to me. Thanks again. Can't wait to get home and further my endeavour. Just arrived at work, though.

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-03, 17:17

Works on youtube now, too. And fine - at least for me.
On google it's too ambitious. A lot of that menu-stuff is made with a-elements. :) I'll differentiate more.
For the <form> I tried with a listener for "submit", but that never gets called. Is this event not available in PM?
(Meanwhile I'll try somtething else.)

Now the ugly. On github I get this security warning

Code: Select all

[17:55:03.386] Content Security Policy: The page's settings blocked the loading of a resource: An attempt to call JavaScript from a string (by calling a function like eval) has been blocked @ sbapi/sandbox.js:187
According to wikipedia/CSP#add-ons that shouldn't happen. Get one for uBlock, too - but about inline scripts. Is that a PM-bug or do those two add-ons do something special or has the CSP-specification changed?

code:

Code: Select all

// ==UserScript==
// @name           Revive History
// @namespace      DjogaRo
// @description    Userscript to counter evil event listeners.
// @include        https://www.youtube.com*
// @include        https://github.com*
// @include        http://www.google.com/?*
// @include        http://www.google.de/?*
// @include        https://www.google.com/?*
// @include        https://www.google.de/?*
// @include        http://*.google.*/*q=*
// @include        https://*.google.*/*q=*
// @run-at         document-start
// ==/UserScript==

console.log("__test");

window.addEventListener("click", function (evt) {
  var el = evt.target;
  while ( el.tagName !== "A" && (el = el.parentNode) );
  if ( el.tagName === "A" ) {
    if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
    if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
  }
}, true);

window.addEventListener("submit", function (evt) {
	console.log("__submitted");
  if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
  if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
}, true);

User avatar
ketmar
Lunatic
Lunatic
Posts: 365
Joined: 2015-07-28, 11:10
Location: Earth

Re: How to enforce traditional behaviour on clicking Links?

Post by ketmar » 2015-11-03, 18:41

DjogaRo wrote:Now the ugly. On github I get this security warning...
that was the bugfix for firefox, taken from GreaseMonkey. as GS doesn't support firefox at all, and Pale Moon doesn't require that bugfix — bye-bye, back to sane code. i released updated GS version, it shouldn't have such problem.

as for the source of the message: it's not a bug per se. GS really used inline script in one place (well, technically it is inline script from the browser PoV), so Pale Moon blocked that according with CSP. so, yes: addons doing special things, and Pale Moon is completely right. ;-)

i didn't noticed the bug ealier 'cause i turned CSP off (introducing that into web wasn't a good idea, i believe), and i have javascript globally disabled, with short whitelist. anyway, should be fixed now.

User avatar
New Tobin Paradigm
Knows the dark side
Knows the dark side
Posts: 8850
Joined: 2012-10-09, 19:37
Location: Skaro

Re: How to enforce traditional behaviour on clicking Links?

Post by New Tobin Paradigm » 2015-11-03, 18:47

Off-topic:
When you develop and test stuff Ketmar, you should use a default profile for confirmation and put yourself into the shoes of the intended end user. I have made this mistake my self and it has always came back and bit me on the ass because I did.
How far are you prepared to go? How much are you prepared to risk? How many people are you prepared to sacrifice for victory?
Are you willing to die friendless, alone, deserted by everyone? Because that's what may be required of you in the war that is to come.

Image

User avatar
ketmar
Lunatic
Lunatic
Posts: 365
Joined: 2015-07-28, 11:10
Location: Earth

Re: How to enforce traditional behaviour on clicking Links?

Post by ketmar » 2015-11-03, 18:53

Off-topic:
Matt A Tobin wrote:When you develop and test stuff Ketmar, you should use a default profile for confirmation and put yourself into the shoes of the intended end user. I have made this mistake my self and it has always came back and bit me on the ass because I did.
or i just have to wait until someone will report a bug, and then i'll fix it. work both ways. ;-)

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-03, 21:04

ketmar wrote:i released updated GS version, it shouldn't have such problem.
Wonderful. Trouble's gone, works on github, too.

All the google breakage I recognized I fixed. Works. Plus I found an easy way to prevent google's exchange of result urls with google urls, that redirect.

Still have to handle the form (search text field thingy), though.
Does anyone know for sure, whether PM fires "submit" events or not? And if so, which is it? :)

User avatar
ketmar
Lunatic
Lunatic
Posts: 365
Joined: 2015-07-28, 11:10
Location: Earth

Re: How to enforce traditional behaviour on clicking Links?

Post by ketmar » 2015-11-03, 21:15

mdn says this. also note that it may not fire event if script is calling form's `submit()` method directly (i didn't checked that, though). so you may not get your event if you attached intercepting event handler to window, you have to wait for page to be loaded and then attach your handler to the form itself.

also, page can use ajax aka XHR to simply send data without triggering `submit()` at all.

i think you have to decipher page scripts to find what is really going on there.

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-03, 21:58

ketmar wrote:mdn says this. also note that it may not fire event if script is calling form's `submit()` method directly (i didn't checked that, though). so you may not get your event if you attached intercepting event handler to window, you have to wait for page to be loaded and then attach your handler to the form itself.
That's why I was asking. If PM doesn't know that event, a sane solution wouln't have to be ruled out, yet. ;)

ketmar wrote:also, page can use ajax aka XHR to simply send data without triggering `submit()` at all.

i think you have to decipher page scripts to find what is really going on there.
Yeah, feared so. Hideous ajax. On another day.

BTW, what I've got so far

Code: Select all

// ==UserScript==
// @name           Revive History
// @namespace      DjogaRo
// @description    Userscript to counter evil event listeners.
// @include        https://www.youtube.com*
// @include        https://github.com*
// @include        http://www.google.com/?*
// @include        http://www.google.de/?*
// @include        https://www.google.com/?*
// @include        https://www.google.de/?*
// @include        http://*.google.*/*q=*
// @include        https://*.google.*/*q=*
// @run-at         document-start
// ==/UserScript==

console.log("__test");

// google special
if ( window.location.hostname.indexOf('.google.') > -1 ) {
  
  window.addEventListener("click", function (evt) {
    var el = evt.target;
    while ( el.tagName !== "A" && (el = el.parentNode) );
    var pa = el.parentElement;
    if (    el
         && el.tagName === "A"
         && el.pathname.indexOf('/search') === 0
         && pa.className.indexOf('action-menu ') === -1 ) {  // that <Space> in there is important
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);

  // Off-Topic
  // prevent google from replacing result url with google url
  window//.document.getElementById('search') // too early I guess - need another listener to add this one
                 .addEventListener("mousedown", function (evt) {
    var el = evt.target, pa = el.parentElement;
    if (    el
         && el.tagName === "A"
         && pa.tagName === "H3"
         && pa.className === "r" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);

  // doesn't work that way
  window.addEventListener("submit", function (evt) {
    console.log("__submitted");
    if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
    if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
  }, true);
  
} else {
  
  window.addEventListener("click", function (evt) {
    var el = evt.target;
    while ( el && el.tagName !== "A" && (el = el.parentNode) );
    if ( el.tagName === "A" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);
  
}

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-04, 23:04

It's slowly getting somewhere. If someone needs to kill time, that's the way to go. :)
When I managed to identify another important event and take it away from evil google scripts, a follow-up event emerged - previously prevented from eventing by said evil scripts, I guess. But one first has to notice that. Dumb luck, when you are sniffing for keyboard events and the next important one in line is a mouse event. I didn't touch no mouse, I swear. Strange world.

Whatever. It also works for google's search form, now (either when hitting <Enter> or clicking on the search button), but not yet for clicking items on its dropdown-menu.

code:

Code: Select all

// ==UserScript==
// @name           Revive History
// @namespace      DjogaRo
// @description    Userscript to counter evil event listeners.
// @include        https://www.youtube.com*
// @include        https://github.com*
// @include        http://www.google.com/?*
// @include        http://www.google.de/?*
// @include        https://www.google.com/?*
// @include        https://www.google.de/?*
// @include        http://*.google.*/*q=*
// @include        https://*.google.*/*q=*
// @run-at         document-start
// ==/UserScript==

console.log("__test");

// google special
if ( window.location.hostname.indexOf('.google.') > -1 ) {
  
  window.addEventListener("click", function (evt) {
    var el = evt.target;
    while ( el.tagName !== "A" && (el = el.parentNode) );
    if (    el
         && el.tagName === "A"
         && el.pathname.indexOf('/search') === 0
         && el.parentElement.className.indexOf('action-menu ') === -1 ) {  // that <Space> in there is important
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);

  // Off-Topic
  // prevent google from replacing result url with google url
  window//.document.getElementById('search') // too early I guess - need another listener to add this one
                 .addEventListener("mousedown", function (evt) {
    var el = evt.target, pa = el.parentElement;
    if (    el
         && el.tagName === "A"
         && pa.tagName === "H3"
         && pa.className === "r" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);

// for search form
  window.addEventListener("keydown", function (evt) {
    if (    evt.key === "Enter"
         && (el = evt.target)
         && el.tagName === "INPUT"
         && el.form.offsetParent.id === "searchform" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);
  
  window.addEventListener("click", function (evt) {
    console.log('click_0');
    console.log(evt.target.tagName);
    console.log(evt.target.form.offsetParent.id);
    if (    (el = evt.target)
         && el.tagName === "BUTTON"
         && el.form.offsetParent.id === "searchform" ) {
      console.log('click_yeah');
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);
  
  window.addEventListener("submit", function (evt) {
    if (    (el = evt.target)
         && el.tagName === "FORM"
         && el.offsetParent.id === "searchform" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);
// end for search form
  
} /* end google special */ else {
  
  window.addEventListener("click", function (evt) {
    var el = evt.target;
    while ( el && el.tagName !== "A" && (el = el.parentNode) );
    if ( el.tagName === "A" ) {
      if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
      if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
    }
  }, true);
  
}

DjogaRo

Re: How to enforce traditional behaviour on clicking Links?

Post by DjogaRo » 2015-11-07, 11:33

The form too works now as intended including that drop-down-autocomplete-search-suggestion thingy (whatever it's called). So, on the usual search page all works fine - at least for me. (I'm not loggin in with an account at google's).
And the "I don't need google to redirect me to the url they show me"-repair got even simpler.

I started to tackle the search form on youtube, but it got weird. I've got a feeling youtube's SPF is based on yet a much geaterer idea than the ajax solution on google.com - or I should look into it again, when I'm actually awake. For now I just use uBlock to not load spf.js - breaking some things like auto-advancing in a playlist.

code:

Code: Select all

// ==UserScript==
// @name           Revive History
// @namespace      DjogaRo
// @description    Userscript to counter evil event listeners.
// @include        https://www.youtube.com*
// @include        https://github.com*
// @include        http://www.google.com/?*
// @include        http://www.google.de/?*
// @include        https://www.google.com/?*
// @include        https://www.google.de/?*
// @include        http://*.google.*/*q=*
// @include        https://*.google.*/*q=*
// @run-at         document-start
// ==/UserScript==

console.log("__test - scripts gets loaded");

window._hideEvent = function(evt) {
  if ( typeof(evt.stopPropagation) === "function" ) evt.stopPropagation();
  if ( typeof(evt.stopImmediatePropagation) === "function" ) evt.stopImmediatePropagation();
};

if ( window.location.hostname.indexOf('.google.') > -1 ) {
// google special
  
// search form
  // to get the form's submission through
  window.addEventListener("keydown", function (evt) {
    if (    evt.key === "Enter"
         && (el = evt.target)
         && el.tagName === "INPUT"
         && el.form.offsetParent.id === "searchform" ) window._hideEvent(evt);
  }, true);
  
  // to get the form's submission through
  window.addEventListener("submit", function (evt) {
    if (    (el = evt.target)
         && el.tagName === "FORM"
         && el.offsetParent.id === "searchform" ) window._hideEvent(evt);
  }, true);
  
  // for the drop-down search suggestions
  // - to prevent google from reacting to the user's selection (with mouse)
  window.addEventListener("mouseup", function(evt) {
    var el = evt.target;
    if ( el.tagName === "B" ) el = el.parentNode;
    if (    el.tagName === "DIV"
         && el.className === "sbqs_c" ) window._hideEvent(evt);
  }, true);

  window.addEventListener("click", function (evt) {
    var el = evt.target;
    
    // to get the form's submission through
    if (    el.tagName === "BUTTON"
         && el.form.offsetParent.id === "searchform" ) {
      window._hideEvent(evt);
      return;
    }
    
    // for the drop-down search suggestions
    // - make it update the form and submit
    if ( el.tagName === "B" ) el = el.parentNode;
    if (    el.tagName === "DIV"
         && el.className === "sbqs_c" ) {
      window._hideEvent(evt);
      var form = window.document.forms['f'];
      form.elements['q'].value = el.textContent;
      form.submit();
      return;
    }
// end search form
    
    // links with changed search parameters shall not be handled through XHRs
    while ( el.tagName !== "A" && (el = el.parentNode) );
    if (    el
         && el.tagName === "A"
         && el.pathname.indexOf('/search') === 0
         // this is to exclude the little green triangle under a result's headline
         // following that href doesn't make sense
         // that <Space> in there is important
         // - don't want to match 'action-menu-*'
         && el.parentElement.className.indexOf('action-menu ') === -1 )
      window._hideEvent(evt);
  }, true);

// Off-Topic
  // prevent google from replacing external urls with redirecting google urls
  window//.document.getElementById('search') // too early I guess - need another listener to add this one
                 .addEventListener("mousedown", function (evt) {
    var el = evt.target;
    while ( el && el.tagName !== "A" && (el = el.parentNode) );
    if (    el
         && el.tagName === "A"
         && typeof(el.onmousedown) === "function" )
      el.onmousedown = null;
  }, true);
// end Off-Topic

// end google special

} else if ( window.location.hostname.indexOf('.youtube.') > -1 ) {
  
  /*                                              */
  /* For now I just use uBlock to not load spf.js */
  /*                                              */
  
// // youtube special
  
// // search form

  // // to get the form's submission through
  // window.addEventListener("keypress", function (evt) {
    // if (    evt.key === "Enter"
         // && (el = evt.target)
         // && el.id === "masthead-search-term" ) window._hideEvent(evt);
  // }, true);
  // window.addEventListener("keyup", function (evt) {
    // if (    evt.key === "Enter"
         // && (el = evt.target)
         // && el.id === "masthead-search-term" ) window._hideEvent(evt);
  // }, true);
  // window.addEventListener("change", function(evt) {
    // if ( el.id === "masthead-search-term" ) window._hideEvent(evt);
  // }, true);
  
  // window.addEventListener("click", function(evt) {
    // var div = (el = evt.target);
    // while ( div && div.tagName !== "DIV" && (div = div.parentNode) );
    // if ( div.tagName === "DIV" && div.className === "gsq_a" ) {
      // window._hideEvent(evt);
      // console.log("suggestion clicked");
      // if      ( el.tagName === "B"  ) el = el.parentElement;
      // else if ( el.tagName === "td" ) el = el.firstElementChild;
      // var form = window.document.forms['masthead-search'];
      // form.elements['masthead-search-term'].value = el.textContent;
      // form.submit();
    // }
  // }, true);
// // end search form
  
  // window.addEventListener("click", function (evt) {
    // var el = evt.target;
    // while ( el && el.tagName !== "A" && (el = el.parentNode) );
    // if ( el && el.tagName === "A" ) window._hideEvent(evt);
  // }, true);
  
// // end youtube special

} else {
  
  window.addEventListener("click", function (evt) {
    var el = evt.target;
    while ( el && el.tagName !== "A" && (el = el.parentNode) );
    if ( el && el.tagName === "A" ) window._hideEvent(evt);
  }, true);
  
}
Off-topic:
On google and youtube I have an issue with images not showing. It is a problem with my profile (a clean one doesn't have the issue). And I ruled out userscript, uBlock, and uMatrix - the other active add-ons I deem not likely to cause this.
It seems to be about images that are specified to be 1px * 1px in size and to be scaled up, and maybe gifs only.

Does someone have an idea about a browser setting, that might cause my trouble?

Locked