Chrome扩展程序:为什么我无法将变量传递给函数(JS)

时间:2019-06-26 11:42:56

标签: javascript google-chrome-extension

我是JavaScript新手。 我正在使用Chrome扩展程序。

我需要从页面(Vtiger CRM)解析项目名称,然后在Trello中创建Card,该卡的名称必须是已解析的项目名称。 (对不起,我的英语)。

manifest.json

    {
    "manifest_version": 2,
    "name": "Roonyx VTIGER optimizer",
    "version": "0.0.1",
    "description": "Создание доски в Trello и чата в Discord",
    "icons": {
        "64": "icon.png"
    },
    "browser_action": {
        "default_icon": "icon.png",
        "default_title": "Roonyx VTIGER optimizer",
        "default_popup": "popup.html"
    },
    "web_accessible_resources": [
        "settings/index.html",
        "popup.html"
    ],
    "options_page": "settings/index.html",
    "content_scripts": [
        {
            "js": [
                "scripts/jquery-2.1.1.js",
                "scripts/client.js",
                "scripts/key.js",
                "scripts/settings.js",
                "scripts/hashSearch.js"
            ],
            "run_at": "document_idle",
            "matches": [ "<all_urls>" ]
        }
    ],
    "background": {
      "scripts": ["scripts/background.js"],
      "persistent": false
    },
    "permissions": ["storage", "identity", "tabs", "<all_urls>"]
}

这是我的background.js代码的一部分:

var myProject;

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
      window.myProject = $(request.content).find(".projectname").text(); 
    });

chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.executeScript(tab.id, {
        code: "chrome.extension.sendMessage({content: document.body.innerHTML}, function(response) { console.log('success'); });"
    }, function() { console.log('done'); });
});

// Trello Creating Card

var APP_KEY = '*app_key*';
var myList = '5d1089c67893fe7afb014694';

function trelloInit() {
    Trello.setKey(APP_KEY);
    Trello.setToken(localStorage.getItem('trello_token'));
}

var creationSuccess = function (data) {
    console.log('Card created successfully.');
    console.log(JSON.stringify(data, null, 2));
    console.log(myProject);
};

var newCard = {
    name: myProject, 
    desc: 'This is the description of our new card.',
    // Place this card at the top of our list 
    idList: myList,
    pos: 'top'
};

function createCard() {
    $("#trello_create_card").click(function () {
        trelloInit();
        Trello.post('/cards/', newCard, creationSuccess);     
    });
}

$(document).ready(createCard);

当脚本在creationSuccess函数中执行“ console.log(myProject)”时,我在控制台中看到了所需的项目名称。

但是我在Trello中看到创建的卡具有空白名称。 我已经尝试在newCard中使用window.myProject,但没有任何变化...

如果需要查看整个设置,则可以在GitHub上查看回购: https://github.com/AnatolySt/chrome-roonyx

有人可以解释我在做什么错吗? 预先谢谢你

0 个答案:

没有答案