userscript (violentmonkey) 脚本在第一页上运行,不在后续页面上运行

问题描述 投票:0回答:1

我正在尝试制作一个用户脚本,该脚本将在新选项卡中打开亚马逊的发票,以便我可以尝试更好地对我的支出进行分类。这是皮塔饼。

链接如下: https://www.amazon.com/cpe/your payments/transactions

我的脚本在第一页上运行,但是当我按底部的“下一页”按钮时,脚本无法在新页面上运行。我尝试了不同的

@run-at
选项,但它们没有什么区别。

我认为是由于某种动态生成或加载后续页面,但我不知道如何挂钩,而且我似乎没有以我得到提示的方式来表达我的搜索词如何帮助它。

// ==UserScript==
// @name        Open payment invoices in new tab
// @namespace   Violentmonkey Scripts
// @match       https://www.amazon.com/cpe/yourpayments/transactions*
// @grant       none
// @version     1.0
// @author      -
// @description 2/5/2024, 12:24:35 PM
// ==/UserScript==


var TargetLink = document.querySelectorAll('.a-span12 a');

var len = TargetLink.length;

for(var i=0; i<len; i++)
{
   TargetLink[i].setAttribute('target', '_blank');
}
javascript tampermonkey userscripts
1个回答
0
投票

这是有效的代码。感谢@wOxxOm 指明了方向。

// ==UserScript==
// @name        Open payment invoices in new tab
// @namespace   Violentmonkey Scripts
// @match       https://www.amazon.com/*
// @grant       none
// @version     1.0
// @author      -
// @description 2/5/2024, 12:24:35 PM
// ==/UserScript==


window.addEventListener("click", function(evnt){
    if (evnt.target.matches('.a-span12 a'))  {
      evnt.target.setAttribute('target', '_blank');
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.