【问题标题】:Safari alternative for lookbehind regex with TampermonkeySafari 替代使用 Tampermonkey 的后向正则表达式
【发布时间】:2021-04-26 10:18:50
【问题描述】:

我正在尝试将我在 Chrome 中使用的脚本用于 safari。它使用后向正则表达式来跳过页面,但 Safari doesn't support that。它可以识别亚马逊 ASIN 并将链接放在一起。

这是我在网上某处找到的原始代码;

// ==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"

})();

我尝试按照this question 中的建议替换后向正则表达式;

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

但是当我替换这些 URL 时,URL 会变得混乱,并且在扩展名之前会有 tld=

要测试此脚本,您可以使用this one 之类的网址。

【问题讨论】:

  • 如果您捕获组包括您在后视中拥有的内容(例如asin=.+)然后在构建finalSite时将其从匹配中删除(例如mAsin[0].slice(5)mAsin[0].replace('asin=', ''))?
  • ...或者你可以使用URL.searchParams(),如果你不需要支持旧浏览器
  • @secan 如果我从 finalSite 中删除 mCountry,那么 URL 将不完整?

标签: javascript safari tampermonkey lookbehind


【解决方案1】:

这就是我在上面评论中的意思:

// ==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"
})();

假设网址是https://partalert.net/product.js?asin=B08H93GKNJ&amp;price=%C2%A3335.73&amp;smid=A3P5ROKL5A1OLE&amp;tag=partalert-21&amp;timestamp=07%3A22%20UTC%20%2826.4.2021%29&amp;title=Xbox%20Series%20X&amp;tld=.co.uk;那么mAsyn 将是'asin=B08H93GKNJ'mCountry 将是'tld=.co.uk'。使用slice(5),您会得到两个字符串,而没有asin=tld=. 位。

换句话说,不是尝试直接捕获 URL 参数值('B08H93GKNJ''co.uk'),而是首先捕获整个 'key=value' 子字符串并在第二步中删除 'key=' 部分。

附:由于您的原始代码未包含任何检查 mAsinmCountry 是否已定义且不是空字符串,因此我也没有插入它们,但您可能需要考虑实施这些检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2017-05-12
    • 2022-01-11
    • 1970-01-01
    • 2021-11-10
    • 2010-10-05
    • 1970-01-01
    相关资源
    最近更新 更多