简体   繁体   English

在开发greasemonkey脚本时如何检查值和字段?

[英]How do I inspect values and fields while developing a greasemonkey script?

Probably a very silly question, but I'm trying to make a simple thing like this:可能是一个非常愚蠢的问题,但我正在尝试做这样一个简单的事情:

var links = document.querySelectorAll ("a[href*='pattern'");
for (var J = links.length - 1;  J >= 0;  --J) {
    var link  = links[J];
    let myvar = "foobar";
    link.href = `https://example.com${myvar}`
}

This is just simple text replacement, and the above code works in the sense that it does replace the links.这只是简单的文本替换,上面的代码确实替换了链接。

But what I find a mess is that my development feels so much like just trial and error.但我发现一团糟的是,我的开发感觉就像是反复试验。 I modify the code.我修改代码。 If nothing happens on the page when I update, then I know that's something is wrong.如果我更新时页面上没有任何反应,那么我知道有问题。 But this is extremely inefficient.但这是极其低效的。 My procedure is something like trying code like this:我的程序就像尝试这样的代码:

var myvar=link.text;
alert(myvar);

Then I update the page.然后我更新页面。 If nothing shows up at all, then link probably didn't have a field called text.如果根本没有显示任何内容,则链接可能没有名为 text 的字段。 I realize that people developing greasemonkey scripts for certain are doing this in a much more efficient manner, via a console or something.我意识到开发油脂猴脚本的人正在以更有效的方式通过控制台或其他方式执行此操作。 But I just cannot figure out where to start.但我就是不知道从哪里开始。 How would I approach the above?我将如何处理上述问题? How to figure out available fields and methods in an object?如何找出对象中可用的字段和方法? How to inspect their values?如何检查它们的值?

Here is an example based on your code ...这是一个基于您的代码的示例...

Updated based on comment根据评论更新

// To view logged message, open Developer tools for THAT page 
// e.g. F12 in Firefox/Chrome

const myvar = 'foobar';

// get the target links
const links = document.querySelectorAll('a[href*="pattern"');
console.log('links', links);

// run a loop on the links
links.forEach(a => { 
 
  console.log('link url before change', a.href);
  a.href = 'https://example.com' + myvar
  console.log('link url after change', a.href);

  // if you need link text
  console.log('link text', a.textContent);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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