0

I am trying to inject code after detecting bookmark created .

**manifest.json**

"manifest_version": 3,
  "background": {
  "service_worker": "background.js"
  },
  "permissions": ["storage", "activeTab", 
  "scripting","pageCapture","bookmarks","tabs","contextMenus"],
  "host_permissions": ["http://localhost/*", "*://*/*"],

**Background.js**

function greeting() {
  console.log("dynamic content script injected");
  document.body.style.backgroundColor = 'orange';
}

chrome.bookmarks.onCreated.addListener( () => {
  console.log("Bookmark created");

      chrome.tabs.query({currentWindow: true, active: true}), function (tabArray) {
      chrome.scripting.executeScript(
      {
        target: {tabId: tabArray[tabArray.length - 1].id},
        function : greeting
      })
  }
  
});

The function "greeting" never gets executed. I did try lastFocusedWindow: true property in chrome.tabs.query() also but it still does not run.

Hanu
  • 57
  • 6
  • 1
    Open [background.js console](/a/10258029) and see if there's an error. I guess your current window is devtools, which triggers a bug in Chrome so it returns an empty tabArray. – wOxxOm Apr 05 '22 at 10:55
  • @wOxxOm I don't see any error at all when i open background.js console. So I closed that console , hoping currentwindow will be the tab window on which bookmark icon is clicked. I expect `console.log("dynamic content script injected"); document.body.style.backgroundColor = 'orange'; ` to be executed but they don't. I put breakpoint at chrome.tabs.query({currentWindow: true, active: true}), function (tabArray) at this function and i see it jumps away from this block which mean currentWindow and active may be false. why it does not execute the block inside it? Thanks @wOxxOm – Hanu Apr 06 '22 at 06:03
  • Is the tab a web site with a http/https URL? Was the site already bookmarked (the star is filled)? – wOxxOm Apr 06 '22 at 06:06
  • @wOxxOm yes with https. no i did try on many new bookmarks. – Hanu Apr 06 '22 at 06:08
  • 1
    You have a typo: the `)` before function(abArray) should be moved after the lone closing `}` below. – wOxxOm Apr 06 '22 at 06:19
  • @wOxxOm yes . thank you very much. I relied too much on auto completion . can you write the same as answer so I can vote it. – Hanu Apr 06 '22 at 06:44

1 Answers1

0

It was just a typo . I relied too much on auto completion.

Thanks wOxxOm for pointing it out.

You have a typo: the ) before function(abArray) should be moved after the lone closing } below.

cursorrux
  • 1,382
  • 4
  • 9
  • 20
Hanu
  • 57
  • 6