0

I'm trying to make a script that I use in Chrome work for safari. It uses a lookbehind regex to skip a page, but Safari doesn't support that. It recognises the amazon ASIN and puts together a link.

This is the original code I found somewhere on the web;

// ==UserScript==
// @name         PartAlert
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  try to take over the world!
// @author       You
// @match        https://partalert.net/*
// @icon         https://www.google.com/s2/favicons?domain=partalert.net
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


    var url = window.location.href;
    var regexAsin= RegExp("(?<=asin=)(.+)(?=&price)");
    var regexCountry= RegExp("(?<=tld=.)(.+)");
    var mAsin = url.match(regexAsin);
    var mCountry = url.match(regexCountry);

    var finalSite = "https://www.amazon."+ mCountry[0]+ "/dp/" + mAsin[0] + "?tag=test";

   // window.location.href = finalSite;
    window.location.href = finalSite + "&psc=1&aod=1&condition=all"

})();

I tried replacing the lookbehind regex as suggested in this question;

var regexAsin= RegExp("(?:asin=)(.+)(?=&price)");
    var regexCountry= RegExp("(?:tld=.)(.+)");

but when I replace those the URL gets messed up and will have tld= before the extension.

To test this script you could use a url like this one.

  • What if you capture the groups _including_ what you have in the look-behind (e.g. `asin=.+`) and then remove it from the match when you build `finalSite` (e.g. `mAsin[0].slice(5)` or `mAsin[0].replace('asin=', '')`)? – secan Apr 26 '21 at 10:34
  • ... or you could use [`URL.searchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams#examples), if yu do not have to support older browsers – secan Apr 26 '21 at 10:40
  • @secan if I remove mCountry from the finalSite, then the URL will not be complete? – user091627 Apr 26 '21 at 10:56

1 Answers1

0

This is what I meant in my comment above:

// ==UserScript==
// @name         PartAlert
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  try to take over the world!
// @author       You
// @match        https://partalert.net/*
// @icon         https://www.google.com/s2/favicons?domain=partalert.net
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  var url = window.location.href;
  var regexAsin = RegExp("asin=[^&]+"); // matches 'asin=' followed by one or more characters excluding '&' (which would indicate the beginning of the next URL param)
  var regexCountry = RegExp("tld=[^&]+"); // matches 'tld=' followed by one or more characters excluding '&' (which would indicate the beginning of the next URL param)
  var mAsin = url.match(regexAsin)[0];
  var mCountry = url.match(regexCountry)[0];

  var finalSite = "https://www.amazon." + mCountry.slice(5) + "/dp/" + mAsin.slice(5) + "?tag=test";

  window.location.href = finalSite + "&psc=1&aod=1&condition=all"
})();

Let's say the URL is https://partalert.net/product.js?asin=B08H93GKNJ&price=%C2%A3335.73&smid=A3P5ROKL5A1OLE&tag=partalert-21&timestamp=07%3A22%20UTC%20%2826.4.2021%29&title=Xbox%20Series%20X&tld=.co.uk; then mAsyn will be 'asin=B08H93GKNJ' and mCountry will be 'tld=.co.uk'. With slice(5) you get the two strings without the asin= and tld=. bits.

In other words, instead of trying to capture directly the URL params values ('B08H93GKNJ' and 'co.uk'), at first you capture the entire 'key=value' substrings and remove the 'key=' part in a second step.

P.S. as your original code did not include any check on whether mAsin and mCountry are defined and are not empty strings, I did not inserted them either but you might want to consider implementing those check.

secan
  • 2,622
  • 1
  • 7
  • 24