简体   繁体   English

为什么这个函数在作为书签运行时返回 undefined ?

[英]Why does this function return undefined when run as a bookmarklet?

I'm trying to create a bookmarklet that will alter the styles of an interface I use daily, in order to make it easier to use.我正在尝试创建一个书签,它将改变我每天使用的界面的样式,以使其更易于使用。 However, when I begin the process of minifying the code I simply get the error of undefined in the console.但是,当我开始缩小代码的过程时,我只会在控制台中收到undefined错误。

What am I doing wrong?我究竟做错了什么?

This is my latest attempt.这是我最近的尝试。 Previous attempts are at the bottom.以前的尝试在底部。

javascript:void%20function(){var%20a=document.querySelectorAll(%22.btn%22);a.forEach(function(a){a.setAttribute(%22style%22,%22padding:%2015px%2025px%20!important;%22)})}();

Full:满的:

var matches = document.querySelectorAll('.btn');


matches.forEach(function(item) {
  item.setAttribute("style","padding: 15px 25px !important;");
});

Non URL-encoded bookmarklet:非 URL 编码的书签:

javascript:void function(){var a=document.querySelectorAll(".btn");a.forEach(function(a){a.setAttribute("style","padding: 15px 25px !important;")})}();

The bookmarklet must return undefined , otherwise it won't execute the code properly (it may refresh the page, open the input instead of staying on the page etc). bookmarklet 必须返回undefined ,否则它将无法正确执行代码(它可能会刷新页面、打开输入而不是停留在页面上等)。 So undefined is not an error, it is a normal matter for any bookmarklet, and the only viable option.所以undefined不是错误,它是任何书签的正常问题,也是唯一可行的选择。

The problem comes if the bookmarklet does not do what it is supposed to.如果书签没有按预期执行,问题就会出现。 As for your code, your full code that retrieves elements with className 'btn' seems like working .至于您的代码,检索具有 className 'btn'元素的完整代码似乎可以正常工作

The issue is just the bookmarklet construction, it might be better to use IIFE-construction , ie Immediately Invoked Function Expression:问题只是书签构造,使用IIFE 构造可能会更好,即立即调用函数表达式:

javascript:(function () {
    /*code here*/
})();

Therefore, the working bookmarklet code is supposed to be like this:因此,工作书签代码应该是这样的:

javascript:(function () {
    var matches = document.querySelectorAll('.btn');
    matches.forEach(function(item) {
        item.setAttribute('style','padding: 15px 25px !important;');
    });
})();

And you can copy-paste the code, without much worrying about spaces (like %20 ) or new lines;您可以复制粘贴代码,而不必担心空格(如%20 )或换行符; the code is supposed to get fixed itself upon inserting it as a bookmarklet.代码应该在将其作为书签插入后自行修复。

PS: If someone wishes to make sure it works, you can substitute document.querySelectorAll('*') for document.querySelectorAll('.btn') to apply changes to everything and to see the result right away. PS:如果有人希望确保它有效,您可以将document.querySelectorAll('*')替换为document.querySelectorAll('.btn')以将更改应用于所有内容并立即查看结果。

Here is an example:下面是一个例子:

 /* javascript:(function () {*/ var func1 = function(){ var matches = document.querySelectorAll('.btn'); matches.forEach(function(item) { item.setAttribute('style','padding: 15px 25px !important;'); }); } /* })();*/
 <p><button id='id1' class='btn'>my button1</button></p> <p><button id='id2' class='btn'>my button2</button></p> <p><button id='id3' onclick=func1()>Press to Execute Code</p>

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

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