如何创建开发Chrome扩展程序(例如window.location)的Chrome标签页? [英] How to create chrome tab of develop Chrome Extension like window.location?

查看:144
本文介绍了如何创建开发Chrome扩展程序(例如window.location)的Chrome标签页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发chrome扩展程序. 我有一个requireman,它正在创建一个带有javascript'window'函数等参数的新标签.

I am developing a chrome extension. I have a requiremen that is create a new tab with some params like javascript 'window' function.

默认使用javascript窗口功能,我需要按如下所示设置此代码,然后window.location.replace允许访问新窗口中的页面的权限.

By default javascript window function, i need to set this code as below then window.location.replace allow permission to access the page in new window.

window.name = JSON.stringify({
    id : 'tmp',
    companyCode : companyCode,
    locationCode : locationCode,
    terminalNo : terminalNo,
    terminalKey : terminalKey,

    server:{ 
        protocol : protocol,
        remoteHost : remoteHost,
        remotePort : remotePort,

        clientHost : clientHost,
        localPort : clientPort,
        webContext : null,

        gcRemoteHost : remoteHost,
        gcRemotePort : remotePort,

        soaPort: 9091,
        webSocketPort : 9099,
    } 
});

现在,我正在使用Google chrome API创建标签.

Now, I am using the google chrome api to create tab.

chrome.tabs.create({
   url: new_location
});

所以,我有一个问题,我应该如何通过window.name上方的位置来创建一个可以访问新位置的新标签页.

So, I have a question, How should I pass above window.name to create a new tab that I can access the new location.

推荐答案

注入设置名称的内容脚本代码.

Inject a content script code that sets the name.

  1. 允许在manifest.json中使用URL(更好)或"<all_urls>"(更糟,但有时是不可避免的):

  1. Permit the URL (better) or "<all_urls>" (worse, but sometimes unavoidable) in manifest.json:

"permissions": ["http://www.example.com/*"],

  • 将名称放入变量:

  • Put the name into a variable:

    var windowName = JSON.stringify({
        foo: 'bar',
    });
    

  • 在后台使用 chrome.tabs.executeScript 弹出/选项页面脚本,以内容字符串代码的形式运行内容脚本代码,并在新创建的选项卡中以嵌入的窗口名称作为参数:

  • Use chrome.tabs.executeScript in a background/popup/options page script to run the content script code as a string with embedded window name as a parameter in the newly created tab:

    chrome.tabs.create({
        url: 'http://www.example.com'
    }, function(tab) {
        runContentCode(tab.id, injectedSetWindowName, windowName);
    });
    
    function injectedSetWindowName(name) {
        window.name = name;
    }
    
    function runContentCode(tabId, fn, params) {
        chrome.tabs.executeScript(tabId, {
            code: '(' + fn + ')(' + JSON.stringify(params) + ')',
            runAt: 'document_start',
        });
    }
    

  • 如果您已经有一个自动执行的内容脚本(例如,在manifest.json中声明),请使用消息传递(chrome.tabs. sendMessage )以发送窗口名称并进行相应的设置.

    In case you already have an automatically executed content script (e.g. declared in manifest.json), use messaging (chrome.tabs.sendMessage) to send the window name and set it accordingly.

    这篇关于如何创建开发Chrome扩展程序(例如window.location)的Chrome标签页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆