background.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const menuItems = [
  2. { "id": "1", name: "Google(zh)", url: "http://lens.google.com/uploadbyurl?hl=zh&url=" },
  3. { "id": "2", name: "Google(en)", url: "http://lens.google.com/uploadbyurl?hl=en&url=" },
  4. { "id": "3", name: "qidb", url: "http://iqdb.org/?url=" },
  5. { "id": "4", name: "SauceNAO", url: "http://saucenao.com/search.php?url=" },
  6. { "id": "5", name: "TinEye", url: "http://tineye.com/search?url=" },
  7. ];
  8. const idToUrl = {};
  9. menuItems.forEach(p => {
  10. chrome.contextMenus.create({
  11. id: p.id, contexts: ["image"], title: `${p.id} 在 ${p.name} 搜索图片`
  12. })
  13. idToUrl[p.id] = p.url;
  14. })
  15. chrome.contextMenus.onClicked.addListener((info, tab) => {
  16. var searchUrl = idToUrl[info.menuItemId] + info.srcUrl;
  17. chrome.tabs.sendMessage(tab.id, { type: 'getKeyboardState' }, (response) => {
  18. const { ctrl, shift } = response;
  19. if (shift) {
  20. // Shift+点击:在新窗口打开
  21. chrome.windows.create({ url: searchUrl, });
  22. } else {
  23. chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
  24. const currentTabIndex = tabs[0].index;
  25. if (ctrl) {
  26. // Ctrl+点击:在后台打开
  27. chrome.tabs.create({ url: searchUrl, active: false, index: currentTabIndex + 1, });
  28. } else {
  29. // 普通点击:在当前标签页后面打开并切换过去
  30. chrome.tabs.create({ url: searchUrl, active: true, index: currentTabIndex + 1, });
  31. }
  32. }); //tabs.query
  33. }
  34. }); //sendMessage
  35. });