如何使用 GreaseMonkey 在页面上删除/关闭“不可选择”

How would I remove/turn Unselectable off on a page using GreaseMonkey?

本文关键字:关闭 可选择 不可选择 删除 GreaseMonkey 何使用      更新时间:2023-09-26
编码

有点新手,但想知道如何使用 greasemonkey 脚本删除不可选择的属性。

这就是我目前所拥有的。

var elmLink = document.getAttributeById("unselectable");
elmLink.removeAttribute('unselectable');

在 GreaseMonkey 或 JavaScript 中没有removeAttributeByIdgetAttributeById函数。

不过有一些接近的东西:

您可以使用document.querySelector通过 CSS 选择器查找属性:

假设你想找到一个属性为 unicorna 元素,你会有一个看起来像这样的 CSS 选择器: a[unicorn](不过你不需要这个元素):

var element = document.querySelector('a[unicorn]');

document.querySelector只会返回一个元素,所以如果你有多个需要删除的元素,你必须执行document.querySelectorAll并遍历所有这些元素。

然后,您可以按element.(get|set|remove)Attribute getsetremove属性。

因此,您可能具有以下工作原理:

var elmLink = document.querySelector("[unselectable]");
elmLink.removeAttribute('unselectable');

或者,如果您有多个:

var elements = document.querySelectorAll("[unselectable]");
Array.from(elements).forEach(function(element){
    element.removeAttribute('unselectable');
});

实际上没有getAttributeByID这样的函数,你要找的是document.querySelectorAll。 你可以用它来得到你想要的。

var elmLink = document.querySelectorAll("[unselectable]");

现在这将返回带有不可选择属性的每个元素,因此您需要遍历它们以删除该属性。

elmLink.forEach(function(entry) {
    entry.removeAttribute("unselectable");
});

希望这有帮助