3

I will be very grateful to you for your help! I looked through a lot of similar topics, but these answers did not help me. I create the expansion for google chrome, and I have to make a click on the icon open a new window with textarea, in this text area I have to write a message and send it to first user on list.

My manifest.json:

  {
  "name": "Linkedin Chrome Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "This extension send message to Likedin users",
  "minimum_chrome_version": "26",
  "offline_enabled": true,

  "icons": {
    "16": "img/Spider-16.png",
    "48": "img/Spider-48.png",
    "128": "img/Spider-128.png" },

  "content_scripts": [
    {
      "js": [
        "js/jquery-3.2.0.min.js",
        "js/content.js"
      ],
      "matches": [
        "https://www.linkedin.com/*",
         "<all_urls>"
      ],
      "run_at":
      "document_idle",
      "all_frames": false
    }
  ],
  "background": {
    "persistent": false,
    "scripts": [
      "js/background.js"
    ]
  },
  "web_accessible_resources": ["js/main.js", "index.html"],
  "permissions": [
    "storage",
    "<all_urls>",
    "tabs",
    "activeTab",
    "https://www.linkedin.com/*",
    "*://*/*",
    "notifications"
  ],

  "browser_action": { // broser elements
    "default_title": "Linkedin Extension"
  },
  "externally_connectable": {
    "matches": ["https://www.linkedin.com/*"]
  }
}

background.js

chrome.browserAction.onClicked.addListener(function (tab) {

    // if (tab.url.indexOf('https://www.linkedin.com') == -1) {
    //     alert('Please go to Linkedin.com, and run this extensions');
    // }else{
    chrome.tabs.create({
        url: chrome.runtime.getURL('index.html'),
        active: false
    }, function (tab) {
        chrome.windows.create({
            tabId: tab.id,
            type: 'popup',
            focused: true,
            width: 625,
            height: 400,
            top: 50
        });
    });
    chrome.tabs.executeScript(null, {file: "js/jquery.js"});
    chrome.tabs.executeScript(null, {file: "js/content.js"});
    // chrome.tabs.executeScript(null, {file: "js/main.js"});


});





chrome.runtime.onMessage.addListener(function (message_from_content, sender, sendResponse) {
    if (message_from_content == 'start'){

        chrome.tabs.executeScript(null, {file: "js/main.js"});
       console.log(message_from_content);
    }
});

content.js

document.getElementById('btnStart').onclick = function () {

     var text = document.getElementById('text').value;
    // var message = text.replace( '{name}', 'nick');

   setTimeout(function () {
        chrome.storage.local.set({
            'msg': text
        });
   }, 1000);
};

document.addEventListener('click', function () {
    chrome.runtime.sendMessage('start', function (response) {

    });
}, false);

main.js

setTimeout(function () {
    getData();
}, 1000);

function getData(){
    chrome.storage.local.get(['msg'], function (result) {
        var text_of_message = result.msg;
        //alert(m);
        handler(text_of_message);
    });
}


var list = $("#results-list").find('.result');

function  handler(message) {

    $.each(list, function (key, value) {
        if (key == 0){

            var users_name = $(value).find('.name').find('a').text(); // get name of users

            var new_message = message.replace( '{name}', users_name);

            $(value).find('.action-trigger').find('li').click(); //get button for open dropdown
            $(value).find('.send-message').click(); //click button for send message

            $("#subject").val("we are the best IT company in the world");
            $("#message-body-content").val(new_message);

        }
    });
}

I try get some message if button "btnStart" click, and open file main.js, but I get error in console:

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://mkeifaecfbamfcillngajldbnaedpepg/index.html". Extension manifest must request permission to access this host.
    at chrome-extension://mkeifaecfbamfcillngajldbnaedpepg/js/background.js:33:21 

If you now the solution of this problem plese help, and sorry for my English.

Nick
  • 31
  • 1
  • 2
  • 1
    You can't inject content scripts into pages with URLs which are within your extension (and some other URLs also). You need to include the JavaScript you desire to run using the `src` attribute of ` – Makyen Apr 02 '17 at 21:32
  • Try solutions at https://stackoverflow.com/a/36762581/632951 and https://stackoverflow.com/a/33972163/632951 – Pacerier Aug 10 '17 at 04:36
  • As for lastError, you can catch it with https://stackoverflow.com/a/45603880/632951 – Pacerier Aug 10 '17 at 04:37

0 Answers0