2

EDIT: Why doesn't this work?

@match http://tumblr.com/*
$(document).ready(function() {
    $(img).each(function() {
        var i = $(this).attr("src");
        var n = i.replace("http://", "https://");
        $(this).attr("src", function() {
            return n;
        });
    });
});​

EDIT: To be clear, I DO NOT OWN THE WEBSITE. I want to have images on sites like https://facebook.com/ and https://tumblr.com/ be on https.

W Biggs
  • 663
  • 1
  • 10
  • 18
  • 8
    Is your site itself on https? If so, you could use URL like `'//24.media.thumblr.com/xxx'` and it would work automatically. – Ja͢ck Dec 10 '12 at 16:06
  • 1
    As an alternative, just start your URLs with with `//`, leaving out the protocol. The browser will use whatever the current protocol is. – Brad Dec 10 '12 at 16:07
  • 1
    I do not suggest attempting this, it will result in the images being requested twice, once from http and again from https. use `//` as suggested above. – Kevin B Dec 10 '12 at 16:08
  • @KevinB Maybe not all, but definitely some :) – Ja͢ck Dec 10 '12 at 16:09
  • @KevinB Does this only apply to resources external to your domain or all resources? I've never used it and never noticed when I put my site up with SSL, but I always declare everything with `http://`, so I'm wondering if that's not good practice – Ian Dec 10 '12 at 16:13
  • I don't own the website, I mean for viewing images. Hence, user js. I want to edit the tags via user js, not change the website's code to change the tags. It's mostly for the tumblr dashboard. – W Biggs Dec 10 '12 at 16:13
  • @Ian the double request only happens if you change the location of the image after it has began loading or finished loading. Changing from `http://myurl.com/img.jpg` to `https://myurl.com/img.jpg` is like changing from `http://myurl.com/img.jpg` to `http://yoururl.com/img.jpg`, it's an entirely different location so the browser will re-request it. – Kevin B Dec 10 '12 at 16:16
  • @KevinB Ahh right, sorry sorry. That makes sense, that was silly of me. I guess I was asking about normal script/style resources at the head of your page, which I guess is unrelated to the question, but I misunderstood the point of your comment :) – Ian Dec 10 '12 at 16:21
  • There is one major flaw in this plan. The root directory for the `htpps` protocol could be something entirely different. I believe the root directory for the `http` protocol is mostly `public_html` and the root directory for the `https` protocol is mostly `private_html`. Unless all sites you're going to try this copied their site to `priate_html` (or whatever their `https` root directory is) this won't work. – 11684 Dec 10 '12 at 16:33
  • I mostly want this to work on tumblr.com, where they are in the same directory. It should work, but the images aren't changing. – W Biggs Dec 10 '12 at 17:21

2 Answers2

2

hey man it's so simple as far i can understand that you want! You want to change all images src?

$(document).ready( function() {
$("img").each( function() {
var i = $(this).attr("src");
var n = i.replace("http://", "https://");
$(this).attr("src", function() {
return n;
});
});
});
Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • hey man it will change only src but it depends on the domain of tumblr whether it supports https:// secure or not! my hands off from this answer now! – Muhammad Talha Akbar Dec 10 '12 at 16:15
  • @WilsonBiggs This should work, but note that this will require every image to be requested twice, as noted in your question's comments. But it might be the best type of solution for your situation where you cannot modify the HTML – Ian Dec 10 '12 at 16:22
  • yeah @lan I also thought that he have a number of images and instead of wasting his time on changing his HTML images sources one by one manually he wants to change sources of images quickly with the use of little code. But later i noted that the sources he gave were of that domain which do not supports secure connection(https://). So, the sources will not work! – Muhammad Talha Akbar Dec 10 '12 at 16:26
  • I modified it a little so it works as user js, doesn't work... I edited the question with my current script. – W Biggs Dec 10 '12 at 17:26
  • hey man you can't change the domain protocol by only changing the link or source it depends on domain whether it supports this secure connection or protocol feature. TUMBLR.COM does not supports the https:// (secure connection) so, you can't change it by your own. – Muhammad Talha Akbar Dec 10 '12 at 17:33
  • Yes it does. https://24.media.tumblr.com/tumblr_m9uhi132pd1rdc5dfo1_1280.jpg works fine. – W Biggs Dec 10 '12 at 17:35
  • please explain why you need secure connection? – Muhammad Talha Akbar Dec 10 '12 at 17:37
1

The OP was really close, just need to tweak the selector: $(img) to $("img")

$(document).ready(function() {
        $("img").each(function() {
          var link = $(this).attr("src");
          var newLink = link.replace("http://example.com", "//example.com");
          $(this).attr("src", function() {
            return newLink
        });
     });
  });

jQuery requires the use of quotes around DOM element selectors, the OP script would throw img not defined.

iangolden
  • 90
  • 8