Skip to content

Add check for page's referrer before auto download. #1418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 26, 2020

Conversation

mloyd
Copy link
Contributor

@mloyd mloyd commented Jan 12, 2020

This prevents someone from clicking a Google search result leading to an
automatic download that likely is not necessary.

mloyd and others added 2 commits January 12, 2020 09:04
This prevents someone from clicking a Google search result leading to an
automatic download that likely is not necessary.
@mloyd mloyd mentioned this pull request Jan 12, 2020
Copy link
Member

@pedrorijo91 pedrorijo91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for helping us solve this long lasting issue :) a few small comments. Still need to see if there's no problems when running this. Will try to test it ASAP

var ref = document.referrer;
var src = $('#auto-download-link').attr('href');

if (ref && src && ref.indexOf(document.location.origin) === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: can you extract the if condition into a method with a clear name please? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing your point. But it already is in a method named triggerAutoDownload. Well, technically, it was in a method I named autoDownload but then renamed it to the former. If this is not what you meant, please elaborate.

  triggerAutoDownload: function() {
    var ref = document.referrer;
    var src = $('#auto-download-link').attr('href');

    if (ref && src && ref.indexOf(document.location.origin) === 0) {
      $('.downloading .hide').show();
      document.location = src;
    }
  },

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I was not clear, sorry :)

My suggestion was to move the ref && src && ref.indexOf(document.location.origin) === 0 into a method (for instance shouldAutoDownload(ref, src))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand now. Thanks for clarifying. Change made.

var src = $('#auto-download-link').attr('href');

if (ref && src && ref.indexOf(document.location.origin) === 0) {
$('.download .hide').removeClass('hide');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be achieved with $('.download .hide').show()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed per your instruction.


if (ref && src && ref.indexOf(document.location.origin) === 0) {
$('.download .hide').removeClass('hide');
$('<iframe frameborder="0" height="1" width="1"></iframe>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems weird. Not an js expert, but there's probably a better way to achieve what you are trying to do in a cleaner way. What are you achieving with this? Will try to see if there's another solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I just took the iframe markup and included that in the method. Setting the location is a cleaner way to achieve triggering the download.

@peff peff temporarily deployed to git-scm-pr-1418 January 12, 2020 21:10 Inactive
@peff peff temporarily deployed to git-scm-pr-1418 January 13, 2020 14:28 Inactive
@peff peff temporarily deployed to git-scm-pr-1418 January 13, 2020 14:30 Inactive
Copy link
Member

@pedrorijo91 pedrorijo91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for taking so much time @mloyd

the code LGTM, but I wasn't able to find a way to test it. Can I ask you how did you do it?

@mloyd
Copy link
Contributor Author

mloyd commented Jan 23, 2020

Thanks, @pedrorijo91 and sorry for my late reply.

I tested by temporarily changing the masthead view to include four hard-coded links:

<ol>
  <li><a href="http://127.0.0.1:5000/download/win">http://127.0.0.1:5000/download/win</a></li>
  <li><a href="http://127.0.0.1:5000/download/mac">http://127.0.0.1:5000/download/mac</a></li>
  <li><a href="http://localhost:5000/download/win">http://localhost:5000/download/win</a></li>
  <li><a href="http://localhost:5000/download/mac">http://localhost:5000/download/mac</a></li>
</ol>

Then, after firing up scripts/server and browsing to http://localhost:5000, I should be able to confirm by which link I clicked. Clicking links 1 and 2 should not trigger the download. But clicking links 3 and 4 should trigger the download.

@pedrorijo91
Copy link
Member

@peff / @jnavila if any of you wants to give a look just let us know, otherwise I will merge it soon (today or tomorrow probably)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants