Conversaciones » Peticiones de scripts
get all anchor links with specific onclick event and modify it
this should be the code but doesn't work, don't know why:
// ==UserScript==
// @name Innoreader Dashboard
// @description
// @grant
// @include https://www.inoreader.com/dashboard
// @include http://www.inoreader.com/dashboard
// ==/UserScript==
var re = /'subscription','(\d+)'/; // a number in quotes after 'subscription',
window.onload=function() {
var links = document.querySelectorAll("a[onclick]");
for (var i=0; i<links.length;i++) {
var onc = links[i].onclick.toString();
if (onc) {
links[i].setAttribute("sub-id",onc.match(re)[1]);
links[i].onclick = function() {
show_dialog('feed_info_dialog',{subscription_id: this.getAttribute("sub-id")});
return false; // cancel link
}
}
}
}
the problem should be with window.onload=function() {} because with console.log(onc) I get:
function onclick(event) { l('Tree pane','How-to guide'); }
which suggests that the links I want to modify are created afterwards I think, so I need another function I guess
links[i].setAttribute("sub-id",onc.match(re)[1]); is the one failing because the code never reaches the next line links[i].onclick = function() because onc.match is null
Simply intercept the click before it's processed by the site: listen on window
with 3rd parameter set to true
parameter in addEventListener.
// ==UserScript==
// @name Innoreader Dashboard
// @match https://www.inoreader.com/*
// @match http://www.inoreader.com/*
// ==/UserScript==
window.addEventListener('click', event => {
const el = event.target.closest('[onclick*="view_tree_element"]');
const id = el && el.getAttribute('onclick').match(/subscription.*?(\d+)|$/)[1];
if (id) {
el.setAttribute('onclick', `show_dialog("feed_info_dialog", {subscription_id: ${id}})`);
}
}, true);
Note, since the site updates its URL and content without reloading the entire page, you need to include the entire site in @match
or @include
. You can check location.href
inside the click listener if you need to.
it worked somehow, what's that symbol before the show_dialog and after? with single quote the function is changed but the id isn't passed
it worked!!!!!!!!!!!!!!!!!!!!!!!
thanks!!!!!!!!!!!!!!!!
It's a new ES6 template string which is surrounded in backticks and auto-expands ${expression}
within.
f
from the translator I understand that you posted a new question,
well, you can open a new thread, preferably in english
get all anchor links with specific onclick event and modify it
ok, I found what needs to be done for my problem, but I don't know how:
I would like to get all anchor links with view_tree_element('subscription','45592063',1);focusit('1_45592063',true) on onclick event and change it to onclick="show_dialog('feed_info_dialog',{subscription_id: 45592063}