简体   繁体   中英

how to pass variable from chrome.tabs.create

i am new in chrome extensions development, i am going to create a simple extension in which when the main button pressed chrome pass the current open tab's URL to my website, but the problem i can't understand how to pass javascript variables from chrome.tabs.create method??

my mainfest.json file:

{
"manifest_version": 2,

"name": "Fake URL Detection",
"description": "Detect the fake URL when you access it from your browser.",
"version": "1.0",

"permissions": [ "contentSettings", "tabs" ],

"browser_action": {
"default_icon": "icon.png"
},

"background": {
    "scripts": [
        "background.js"
    ]
}
}

my background.js file:

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

chrome.tabs.getSelected(null, function(tab) {
    var tabId = tab.id;
    var tabUrl = tab.url;

    alert(tabUrl);
});
chrome.tabs.create({
    url: "http://mywebsite.com/search?q="+tabUrl
});
});

please help me, how can i pass that tabUrl variable to my external URL

Why are you trying to pass the tabUrl variable to chrome.tabs.create ? You can call the tab.url information in the chrome.tabs.create method.

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.create({
        url: "http://www.google.com/search?q=" + tab.url  //Changed the URL for testing
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM