I'm trying to create a chrome extension that takes a selected text via context menu, appends it to a url and update an existing tab (from the same domain) with the new URL. This is my code:
function soso(search_target){
alert(search_target);
//search_target gets here successfully
chrome.tabs.query({
url: "https://lightning.force.com/lightning/*"},
function(tabs, search_target){
if (tabs[0]){
chrome.tabs.update(tabs[0].id, {
url: "http://www.facebook.com"+search_target, //search_target doesnt get here
active: true
});
}
else{
alert("no window");
}
});
}
function selectionHandler (info, tab)
{
soso( info.selectionText);
}
function resetContextMenus ()
{
chrome.contextMenus.removeAll(
function()
{
var id = chrome.contextMenus.create( {
title: "Open in Salesforce",
contexts: [ "selection" ],
onclick: selectionHandler
} );
}
);
}
resetContextMenus();
The problem is that I cannot pass search_target
to chrome.tabs.update
, I get an undefined
after the URL.
Any ideas on how to pass this variable?
Let's move backwards with our analysis:
chrome.tabs.update
, passing search_target
function
which has a search_target
parameterchrome.tabs.query
function
search_target
in the outer contextfunction
because a parameter with similar name which is undefined shadows itSolution: Remove the parameter to allow the variable of the outer context be known inside the callback function
:
function soso(search_target){
alert(search_target);
//search_target gets here successfully
chrome.tabs.query({
url: "https://lightning.force.com/lightning/*"},
function(tabs){
if (tabs[0]){
chrome.tabs.update(tabs[0].id, {
url: "http://www.facebook.com"+search_target, //search_target doesnt get here
active: true
});
}
else{
alert("no window");
}
});
}
function selectionHandler (info, tab)
{
soso( info.selectionText);
}
function resetContextMenus ()
{
chrome.contextMenus.removeAll(
function()
{
var id = chrome.contextMenus.create( {
title: "Open in Salesforce",
contexts: [ "selection" ],
onclick: selectionHandler
} );
}
);
}
resetContextMenus();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.