如何解决无法弄清楚为什么此代码不编辑 FB 页面中的“href”属性
我想知道为什么这段代码不编辑 facebook href 属性。
我很确定它应该有效。
我在控制台 Error: Promised response from onMessage listener went out of scope
中出错
代码:
// ==UserScript==
// @name facebook anti tracking URL
// @namespace http://tampermonkey.net/
// @version 0.1
// @description remove FB tracking
// @author MévatlavéKraspek
// @match https://www.facebook.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
for (let a of document.querySelectorAll('a')) {
try {
var old_url = a.getAttribute('href');
if (old_url.match(/l\.facebook/)) {
var myRegexp = /.*l\.facebook\.com\/l\.PHP\?u=(.*)\%3Ffbclid.*/;
var match = myRegexp.exec(old_url);
var n = decodeURIComponent(match[1]);
a.setAttribute('href',n);
}
} catch(e) {
true;
}
}
})();
解决方法
我认为您有一个分号导致了问题。
(function() {
'use strict';
for (let a of document.querySelectorAll('a')) {
try {
var old_url = a.getAttribute('href');
if (old_url.match(/l\.facebook/)) {
var myRegexp = /.*l\.facebook\.com\/l\.php\?u=(.*)\%3Ffbclid.*/;
var match = myRegexp.exec(old_url);
var n = decodeURIComponent(match[1]);
a.setAttribute('href',n);
}
} catch(e) {
true;
}; // <---- remove this semi-colon
}
})();
我在 facebook.com(在开发控制台中)运行了以下内容,并且成功了:
for (let a of document.querySelectorAll('a')) {
try {
var old_url = a.getAttribute('href');
console.log(old_url);
} catch(e) {
true;
}
}
由于此代码运行,这可能意味着问题与您的正则表达式有关。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。