Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有没有什么办法通过扩充组件获取设备mac地址? #30

Closed
PikachuWu opened this issue Sep 15, 2017 · 4 comments
Closed

有没有什么办法通过扩充组件获取设备mac地址? #30

PikachuWu opened this issue Sep 15, 2017 · 4 comments
Labels

Comments

@PikachuWu
Copy link

No description provided.

@qazbnm456
Copy link
Member

如果你是 windows 系統的話,可以用如下的方式執行外部指令以獲取 MAC 地址:

var exec = require('child_process').exec;

var regexRegex = /[-\/\\^$*+?.()|[\]{}]/g;

function escape(string) {
    return string.replace(regexRegex, '\\$&');
}

function getMAC(iface) {
    exec("ipconfig /all", function (err, out) {
        if (err) {
            console.warn(err);
            return;
        }
        var match = new RegExp(escape(iface)).exec(out);
        if (!match) {
            console.warn("did not find interface in `ipconfig /all`");
            return;
        }
        out = out.substring(match.index + iface.length);
        match = /[A-Fa-f0-9]{2}(\-[A-Fa-f0-9]{2}){5}/.exec(out);
        if (!match) {
            console.warn("did not find a mac address");
            return;
        }
        return match[0].toLowerCase().replace(/\-/g, ':');
    });
}

@PikachuWu
Copy link
Author

image
我放在扩充组件中运行好像不行

@qazbnm456
Copy link
Member

qazbnm456 commented Sep 15, 2017

這部分錯誤的原因是因為安全性的問題,在 Content Scripts 中不允許 require 函數(否則任何人都可以直接執行任意代碼)。請您將這部分程式碼移到 Background Pages 中去執行,再用 chrome.runtime 相關的 API 傳遞給 Content Scripts ,謝謝。

順帶一提,我發現您好像沒有 Chrome Extensions 相關開發基礎,這邊給您相關教學網站,您有些問題都可以從裡面參考(包括像是我前面提到的 Content ScriptsBackground Pages 的意思):http://ithelp.ithome.com.tw/articles/10187744

最後,因為本專案還在持續開發中,部分 chrome API 也還沒有實作完全,如果您在使用中發現某些 API 有問題,建議您先看一下這個討論串,這是目前 chrome API 的支援程度。

@PikachuWu
Copy link
Author

好的,十分感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants