通过dict数据类型制作特定于标签的变量

时间:2015-07-16 14:54:08

标签: javascript google-chrome google-chrome-extension google-chrome-devtools google-chrome-app

我正在编写一个可以检测来自Coursera页面的视频请求的扩展程序(如果已检测到),然后在单击浏览器操作图标时将其下载。

打开后

https://www.coursera.org/learn/human-computer-interaction/lecture/pE6EB/human-computer-interaction

我使用chrome.webRequest.onBeforeRequest检测视频网址,如果检测到,则将其分配给targetUrl。例如,由于它可能同时打开许多Coursera页面,您可以打开此链接

https://www.coursera.org/learn/human-computer-interaction/lecture/25EPu/the-power-of-prototyping

在您打开最后一个之后立即,targetUrl可能是此页面的视频网址,而不是最后一个。要使targetUrl特定于每个Coursera页面,我将使用字典数据类型来存储选项卡特定变量,键应该是制表符唯一属性,值是相应页面上的视频URL。

我面临的困难是,我不知道如何以及何时创建密钥,任何帮助?

在为targetUrl分配值后创建密钥似乎是合适的,我使用chrome.tabs.getCurrent(function callback)来获取那里的tab属性(background.js中的注释代码),但我刚刚定义对象,谁能解释为什么?

以下是代码

的manifest.json

{
    "manifest_version": 2, 
    "name": "Coursera", 
    "version": "1.0", 
    "minimum_chrome_version": "31", 
    "browser_action": {
        "default_icon": "video-128.png", 
        "default_title": "Click here!"
    }, 
    "description": "I can't has cheezburger!", 
    "permissions": [
        "activeTab", 
        "tabs", 
        "http://*/*", 
        "https://*/*", 
        "cookies", 
        "storage", 
        "management", 
        "downloads", 
        "webRequest", 
        "webRequestBlocking"
    ], 
    "background": {
        "scripts": [
            "background.js"
        ]
    }
} 

background.js

var tabId;
var tabId2targetUrl={};
var targetUrl;

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    targetUrl = details.url;

    // chrome.tabs.getCurrent(function(tab) {console.log(tab.title);});
    // tabId2targetUrl[tabId]=targetUrl;

    console.log(targetUrl);  
},
{
    urls: ["https://*.cloudfront.net/*index.webm*", "https://*.cloudfront.net/*index.mp4*", "http://*/*.aac","http://v.stu.126.net/mooc-video/nos/flv/*",  "http://*/*.mp4"]
});


chrome.browserAction.onClicked.addListener(function(tab) {
    // targetUrl=tabId2targetUrl[tabId];

    var parser = document.createElement('a');
    parser.href = targetUrl;
    var pathname= parser.pathname;
    fileNameExt =pathname.substr(pathname.lastIndexOf('.') );

    chrome.downloads.download({
        url: targetUrl,
        filename: tab.title.split(' - ')[0] + fileNameExt
    },
    function(downloadId) {
        console.log('downloadId '+downloadId);
    });
});

1 个答案:

答案 0 :(得分:1)

tabId应该符合您的目的,实际上是由webRequest / browserAction个事件提供的。

chrome.webRequest.onBeforeRequest.addListener(function(details) {
  targetUrl = details.url;
  tabId2targetUrl[details.tabId]=targetUrl;
},
{
    urls: ["https://*.cloudfront.net/*index.webm*", "https://*.cloudfront.net/*index.mp4*", "http://*/*.aac","http://v.stu.126.net/mooc-video/nos/flv/*",  "http://*/*.mp4"]
});

chrome.browserAction.onClicked.addListener(function(tab) {
  targetUrl=tabId2targetUrl[tab.id];
  /* ... */
});

请注意:当选项卡导航时,您可能希望清除字典条目。

在您的情况下,使用内容脚本按需从页面中提取URL可能会更容易。在这种情况下无需维护词典。

或者,您可以(并且应该!)使用Page Action,在检测到URL后显示它。它将自动清除导航(除非基于pushState的过渡,IIRC)

  

执行将页面操作用于仅适用于几页的功能。