I am writing a chrome extension that shows a contextMenu when there is a selection, which was pretty straight forward:
var item = chrome.contextMenus.create({
"title": "myTitle",
"contexts": ["selection"],
"id": "myItem"
});
Now, I want to create a submenu item for that one, but based on the selected text. I found I was able to do this by adding the callback when it loads, and display the selected text as a submenu item like this:
var item = chrome.contextMenus.create({
"title": "myTitle",
"contexts": ["selection"],
"id": "myItem"
}, function() {
// Create item with the selected text
var subItem = chrome.contextMenus.create({
"title": "%s",
"contexts": ["selection"],
"parentId": "myItem",
"id": "mySubItem"
});
});
This is working, however I want to do something based on the last two characters of the selected string, but I can't. By adding something inside the function like this:
var selection = "%s";
if(selection.splice(-2) == "ab") { }
var subItem...
It doesn't work, because selection.splice(-2)
returns the selected string. It does that, because it's doing "%s".splice(-2) which is just "%s" so it's never actually operating on the selected text. I have verified this because selection.splice(-1)
returns "s", it does not return the last character of what was selected.
How can I work with the selected text here?