我正在尝试右键单击> 按图片搜索网站,但源图片只会是 1 个特定网站,因此网站只需要来自“http://example.com/images1/img1.jpg”的“images1/image1”
var url = 'example.com/search/';
chrome.contextMenus.onClicked.addListener(function(info, tab){
chrome.tabs.create({
url: url + encodeURIComponent(info.srcUrl),
index: tab.index + 1
})
});
var onInitialize = function(){
chrome.contextMenus.create({
title: 'Search by image',
contexts: ['image'],
targetUrlPatterns: ['https://*/*', 'http://*/*'],
id: 'contextMenu'
});
};
此代码生成“example.com/search/https://sourceimageexample.com/images/img1.jpg” 它应该看起来像“example.com/search/images1/img1/ 我在网上找到的解决方案不起作用,或者我无法使其起作用,有什么帮助吗? xD
最佳答案
你可以使用类似这样的东西来 trim 行尾:
let line = 'image/image.jpg';
const suffix = '.jpg';
const result = line.substring(0, line.length - suffix.length);
console.log(result);
关于javascript:从 url 中删除特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49347200/