0

I want to change .jpg or .png to .jpg:orig or .png:orig with greasemonkey ex.https://pbs.twimg.com/media/DMU-nlSV4AAom3a.jpg to https://pbs.twimg.com/media/DMU-nlSV4AAom3a.jpg:orig

// ==UserScript== 
// @name        Twitter Orig jpg
// @namespace   jirat
// @include     https://pbs.twimg.com/*
// @include     https://pbs.twimg.com/media/*
// @version     1
// @grant       none
// ==/UserScript==
document.location.replace(document.location.href.replace(/.jpg/,'.jpg:orig'));

but when it run, it won't stop. It keep going repleace like this

[.jpg:orig:orig:orig:orig]

How can I fix this.

J. Sumittanun
  • 87
  • 1
  • 7

3 Answers3

0

Check if the :orig already exists

let href = document.location.href;

if(href.includes('.jpg') && !href.includes(':orig')){
  document.location.href = href.replace(/.jpg/,'.jpg:orig');
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Use /\.jpg$/ for the regex to match only at the end of the url.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

Setting the location URL for .jpg or .png

function setLocationUrl(){
  let href = document.location.href;
  if(href.lastIndexOf(':orig') == -1 && (href.lastIndexOf('.jpg') || href.lastIndexOf('.png'))){
  let location = href.replace(/.jpg/,'.jpg:orig');
  document.location = location;
  }
}

setLocationUrl();
Ankit Pandey
  • 608
  • 4
  • 17