How do I use promises in a Chrome extension?

What I am trying to do is create a chrome extension that creates new, nested, bookmark folders, using promises.

The function to do this is chrome.bookmarks.create(). However I cannot just
loop this function, because chrome.bookmarks.create is asynchronous. I need to wait until the folder is created, and get its new ID, before going on to its children.

Promises seem to be the way to go. Unfortunately I cannot find a minimal working example using an asynchronous call with its own callback like chrome.bookmarks.create.

I have read some tutorials 1, 2, 3, 4. I have searched stackOverflow but all the questions do not seem to be about plain vanilla promises with the chrome extension library.

I do not want to use a plugin or library: no node.js or jquery or Q or whatever.

I have tried following the examples in the tutorials but many things do not make sense. For example, the tutorial states:

The promise constructor takes one argument—a callback with two
parameters: resolve and reject.

But then I see examples like this:

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

How this works is a mystery to me.

Also, how can you call resolve() when its never been defined? No example in the tutorials seem to match real life code. Another example is:

function isUserTooYoung(id) {
   return openDatabase() // returns a promise   
.then(function(col) {return find(col, {'id': id});})

How do I pass in col, or get any results!

So if anyone can give me a minimal working example of promises with an asynchronous function with its own callback, it would be greatly appreciated.

SO wants code, so here is my non-working attempt:

//loop through all
function createBookmarks(nodes, parentid){  

  var jlen = nodes.length;
  var i;
  var node;
  for(var i = 0; i < nodes.length; i++){
        var node = nodes[i];

        createBookmark(node, parentid);
  }
}

//singular create
function createBookmark(node, parentid){  
      var bookmark = { 
    parentId : parentid,
    index : node['index'],
    title : node['title'],
    url : node['url']
  }

  var callback = function(result){
    console.log("creation callback happened.");
    return result.id;  //pass ID to the callback, too
  }

    var promise = new Promise(function(resolve, reject) {

        var newid = chrome.bookmarks.create(bookmark, callback)

      if (newid){
        console.log("Creating children with new id: " + newid);
        resolve( createBookmarks(bookmark.children, newid));
      }

    });
}

//allnodes already exists
createBookmarks(allnodes[0],"0");

Just doesn't work. The result from the callback is always undefined, which it should be, and I do not see how a promise object changes anything. I am equally mystified when I try to use promise.then().

var newid = promise.then(  //wait for a response?
            function(result){
            return chrome.bookmarks.create(bookmark, callback); 
            }
        ).catch(function(error){
            console.log("error " + error);
        });  

        if (node.children) createBookmarks(node.children, newid);

Again, newid is always undefined, because of course bookmarks.create() is asynchronous.

Chrome.tabs.executeScript(null, {file: ‘inject.js’});

I am trying to make an chrome extension where it injects code into the active tab… When I looked around and did some research I found that
Chrome.tabs.executeScript(null, {file: ‘example.js’});

is what to do… It looks like us so this is the code I’ve chosen to use:

This doesn’t work, obviously inject.js’s code matters so here it is…

document.write('

testing... testing...

');

If my JavaScript isn’t proper please feel free to let me know…

That was just a contexted example to give a simple intro to my problem… Here’s what I’m really doing

Still this but then instead of injecting
click me
I want to inject this into my code —



    
    
    
    

The last part of the problem is that I am having difficulty injecting that code using js because of the quotes inside the code and whatnot… So thanks for all the help

Recap: how to use Chrome.tabs.injectScript(); how to inject HTML with lots of double and single quotes using JavaScript and please provide some working examples…

How do I use data received from native app in html page?

I use native messaging extension. I send a string to native app,so encrypt it in my native app and send encrypted string to web page.

This is my content_script.js:

window.addEventListener("message", function(event)  {
  if (event.source == window &&
      event.data &&
      event.data.direction == "from-page-script") {
    alert("send");
    browser.runtime.sendMessage(event.data);
}
});



function Sign(challenge) {            
            var j_Data = {}
            j_Data["command"] = "sign";
            j_Data["challenge"] = challenge;
            var j_Text = JSON.stringify(j_Data)
            window.postMessage
                ({
                    direction: "from-page-script",
                    message: j_Text,
                }, "*");

            //response = document.getElementById("result").value;
        }


/*
Send a message to the page script.
*/
function messagePageScript(request) {
    var str_Text = request["text"];
    var j_Text = JSON.parse(strText);
    var str_Command = jText["command"];

    if(str_Command == "signed")
    {
        var str_Signed_Data = jText["signeddata"];

    }       
    }


browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    messagePageScript(request);
});

It work correctly.

when I receive a message messagePageScript(request); is called.

How do I use value of str_Signed_Data in my html page?

chrome executeScript not working in chrome but working fine in firefox

In chrome executeScript is not working after tabs.create. Whereas in firefox same is working fine.

chrome.tabs.create({url: "html/output.html"}, function(tab) {
  targetId = tab.id;
  window.close();  
}); 
chrome.tabs.executeScript(null, {file: 'output.js'});

Need Help on Skype Chrome Extension

I wrote a Chrome Extension that can make calls using Skype for web (https://web.skype.com/en/).

I am stuck on below issues:

  • How to send inputs entered in any web page to the extension?
  • How to carry out database post operations within the extension? (so that I can save the logs to our DB)
  • How to open web.skype inside iframe (also within the extension) ?

I can provide further details on above mentioned problems if anyone is interested.

Thank you in advance!

Is it possible to create a google extension that records usernames and passwords when they are put in to the same users google drive?

An extension that saves usernames and passwords to the users google drive or syncs the info to across the users devices?

  • I’m aware this could be used with malicious intentions, but I’m not focused on publishing it to public.

I can edit the content before printing?

The first chrome plugin to work. I do not have detailed information.

Before I print the contents of This link I want to get the specific part.

just how can I print the contents of this table. Is this possible?

chrome extension giving error when installing .crx custom plugin file

I have created an extension. Then I create crx file of extension and install it.
It’s Installed but it ask to repair, and not shown in the tool bar with other extensions.

Thanx

Login with facebook in chrome extension

I want to implement Login with Facebook functionality in my extension.
for that I’ve go through all Facebook document but not find proper solution

Please help me if anyone have implement