Implementing 'Paste' in custom context menu

时间秒杀一切 提交于 2019-12-05 19:51:48

So, in my web application I have a custom context menu which has 'Paste' action (bunch of '<li>' tags in a popup). And when the user click on 'Paste' I call this function

if (browser === 'CHROME') {
                var extensionId = 'some_id';
                chrome.runtime.sendMessage(extensionId, { message: "getClipboardData" },
                    function (clipboardData) {
                        console.log('Clipboard data: ', clipboardData);
                        var txt = $('.helper_textarea');
                        $(txt).val(clipboardData);
                        // Call 'paste' function we have clipboard data
                    }
                );
            }

In my extension I have i paste.js file I have

function getDataFromClipboard() {
  var bg = chrome.extension.getBackgroundPage();
  var helperTextArea = bg.document.getElementById('sandbox');
  if (helperTextArea == null) {
    helperTextArea = bg.document.createElement('textarea');
    document.body.appendChild(helperTextArea);
  }
  helperTextArea.value = '';
  helperTextArea.select();

  // Clipboard data
  var clipboardData = '';

  bg.document.execCommand("Paste");
  clipboardData = helperTextArea.value;
  helperTextArea.value = '';

  return clipboardData;
}

chrome.runtime.onMessageExternal.addListener(
  function(req, sender, callback) {
    if (req) {
      if (req.message) {
         if (req.message == "installed") {
           console.log('Checking is extension is installed!');
           callback(true);
         }
         else if(req.message = "getClipboardData") {
           console.log('Get clipboard data');
           callback(getDataFromClipboard());
         }
       }
    }
    return true;
  }
);

And in manifest file

   "background" : {
     "scripts" : [ "paste.js" ]
   },
   "externally_connectable": {
     "matches": ["*://localhost:*/*"]
   },

and of course

  "permissions": ["clipboardRead" ],

I use this function to check if extension is added

   isExtensionInstalled: function (extensionId, callback) {
        chrome.runtime.sendMessage(extensionId, { message: "installed" },
            function (reply) {
                if (reply) {
                    callback(true);
                } else {
                    callback(false);
                }
            });
    },

And this is working great. Now the problem is how to port this to Edge. What is equivalent to 'chrome.runtime.sendMessage' in Edge? Thanks for your help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!