Suggestion regarding contextMenus.onClicked.addListener

143 views
Skip to first unread message

Smeany Peters

unread,
May 22, 2020, 11:41:20 AM5/22/20
to extensions-dev
Hello Developers,
Is it possible that you could add another value to the onClickData Object 'info' that gives you the title of a right-clicked link instead of solely its url value? As for now is there any workaround?

Thank You!
Smeany

Smeany Peters

unread,
May 22, 2020, 1:51:15 PM5/22/20
to extensions-dev
so i tried to use the js dom query selector which works fine in the console, but implementing it in my extension throws this error:
Error in event handler: Error: Failed to execute 'querySelector' on 'Document': '"a[href='/watch?v=lbK24iwrYbY'][title]"' is not a valid selector.

a working console example when being on this youtube url: https://www.youtube.com/results?search_query=dude
var e = document.querySelector("a[href='/watch?v=eZUKSxE2UZg'][title]");
console.log(e);

my implementation:
chrome.contextMenus.onClicked.addListener(function(info, tab) {
   if (info.menuItemId == "searchYouTube" && info.linkUrl) {
var href = '/' + info.linkUrl.split('/').pop(); //getting only the last part of a youtube url e.g. '/watch?v=eZUKSxE2UZg'
var e = document.querySelector('\"a[href=\''+href+'\'][title]\"');
                //debug dump:
console.log(e);
alert('linkUrl: ' + info.linkUrl + '\n' +
'href' + e.title + '\n' +
href);
searchonyoutube(info);
    }
});

Smeany Peters

unread,
May 22, 2020, 2:16:50 PM5/22/20
to extensions-dev
So during my research i think i got why theres no such easy and  unified property such as title for link elements, because links can merely be a container surrounding any html element such as <p>,<h1>,etc. ... The 'title' attribute i was looking for therefore doesn't need to be set as the links displayed text... For my needs i would have to accomodate my selector to each URL i want to use my extension in. 

Smeany Peters

unread,
May 22, 2020, 2:20:12 PM5/22/20
to extensions-dev
I've found the error in my code as well. I had put too many quotation marks around my query selector. the correct line therefor would be:
var e = document.querySelector('a[href=\''+href+'\'][title]');


Am Freitag, 22. Mai 2020 19:51:15 UTC+2 schrieb Smeany Peters:
Reply all
Reply to author
Forward
0 new messages