通过dict数据类型创建TAB特定变量

make tab specific variable via dict data type

本文关键字:变量 TAB 创建 dict 数据类型 通过      更新时间:2023-09-26

我正在编写一个扩展,可以从Coursera页面检测视频请求,如果检测到,然后在单击浏览器操作图标时下载它。

打开后

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

我使用chrome.webRequest.onBeforeRequest检测视频url,如果检测到,则将其分配给targetUrl。因为它可能同时打开许多Coursera页面,例如,你可以打开这个链接

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

在你打开最后一个之后,那么targetUrl很可能是这个页面的视频url,而不是最后一个。为了使targetUrl特定于每个Coursera页面,我将使用dictionary数据类型来存储标签特定的变量,键应该是标签的唯一属性,值是对应页面的视频url。

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

targetUrl被赋值后创建键似乎是合适的,我使用chrome.tabs.getCurrent(function callback)来获得那里的选项卡属性(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);
    });
});

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)

使用仅对少数页面有意义的功能的页面操作。