简体   繁体   中英

Safari alternative for lookbehind regex with Tampermonkey

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 .

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.

PS 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM