2008-09-19 14 views
11

Có cách nào để tập lệnh của tôi có thể truy xuất các giá trị siêu dữ liệu được khai báo trong tiêu đề của chính nó không? Tôi không thấy bất kỳ điều gì hứa hẹn trong API, ngoại trừ có lẽ GM_getValue(). Điều đó tất nhiên sẽ liên quan đến một cú pháp tên đặc biệt. Tôi đã thử, ví dụ: GM_getValue("@name").Truy cập siêu dữ liệu Greasemonkey từ bên trong tập lệnh của bạn?

Động lực ở đây là tránh đặc điểm dư thừa.

Nếu siêu dữ liệu GM không thể truy cập trực tiếp, có lẽ có cách để đọc nội dung của tập lệnh. Nó chắc chắn trong bộ nhớ ở đâu đó, và nó sẽ không quá khó khăn để phân tích cú pháp cho "// @". (Đó có thể là cần thiết trong trường hợp của tôi bất cứ cách nào, vì giá trị Tôi thực sự quan tâm là @version, đó là một giá trị mở rộng đọc bởi userscripts.org.)

Trả lời

7

Câu trả lời này là hết hạn: Tính đến Greasemonkey 0.9 .16 (Feb 2012) vui lòng xem Brock's answer liên quan đến GM_info


Có. Một ví dụ rất đơn giản là:

var metadata=<> 
// ==UserScript== 
// @name   Reading metadata 
// @namespace  http://www.afunamatata.com/greasemonkey/ 
// @description Read in metadata from the header 
// @version  0.9 
// @include  https://stackoverflow.com/questions/104568/accessing-greasemonkey-metadata-from-within-your-script 
// ==/UserScript== 
</>.toString(); 

GM_log(metadata); 

Xem this thread on the greasemonkey-users group để biết thêm thông tin. Một triển khai mạnh mẽ hơn có thể được tìm thấy gần cuối.

4

Xây dựng dựa trên câu trả lời của Athena, đây là giải pháp tổng quát của tôi mang lại một đối tượng của các cặp tên/giá trị, mỗi cặp đại diện cho thuộc tính siêu dữ liệu. Lưu ý rằng các thuộc tính nhất định có thể có nhiều giá trị, (@include, @exclude, @require, @resource), do đó trình phân tích cú pháp của tôi nắm bắt các mảng đó - hoặc trong trường hợp @resource, như một đối tượng cấp dưới của các cặp tên/giá trị.

 
var scriptMetadata = parseMetadata(.toString()); 

function parseMetadata(headerBlock) 
{ 
    // split up the lines, omitting those not containing "// @" 
    function isAGmParm(element) { return /\/\/ @/.test(element); } 
    var lines = headerBlock.split(/[\r\n]+/).filter(isAGmParm); 
    // initialize the result object with empty arrays for the enumerated properties 
    var metadata = { include: [], exclude: [], require: [], resource: {} }; 
    for each (var line in lines) 
    { 
     [line, name, value] = line.match(/\/\/ @(\S+)\s*(.*)/); 
     if (metadata[name] instanceof Array) 
      metadata[name].push(value); 
     else if (metadata[name] instanceof Object) { 
      [rName, rValue] = value.split(/\s+/); // each resource is named 
      metadata[name][rName] = rValue; 
     } 
     else 
      metadata[name] = value; 
    } 
    return metadata; 
} 

// example usage 
GM_log("version: " + scriptMetadata["version"]); 
GM_log("res1: " + scriptMetadata["resource"]["res1"]); 

Điều này hoạt động tốt trong tập lệnh của tôi.

EDIT: Đã thêm @resource và @require, được giới thiệu trong Greasemonkey 0.8.0.

EDIT: FF5 + tương thích, Array.filter() không còn chấp nhận một biểu thức chính quy

4

Sử dụng the GM_info object, được bổ sung vào Greasemonkey trong phiên bản 0.9.16.

Ví dụ, nếu bạn chạy kịch bản này:

// ==UserScript== 
// @name   _GM_info demo 
// @namespace  Stack Overflow 
// @description  Tell me more about me, me, ME! 
// @include   http://stackoverflow.com/questions/* 
// @version   8.8 
// ==/UserScript== 

unsafeWindow.console.clear(); 
unsafeWindow.console.log (GM_info); 


Nó sẽ ra đối tượng này:

{ 
    version:   (new String("0.9.18")), 
    scriptWillUpdate: false, 
    script: { 
     description: "Tell me more about me, me, ME!", 
     excludes:  [], 
     includes:  ["http://stackoverflow.com/questions/*"], 
     matches:  [], 
     name:   "_GM_info demo", 
     namespace:  "Stack Overflow", 
     'run-at':  "document-end", 
     unwrap:   false, 
     version:  "8.8" 
    }, 
    scriptMetaStr:  "// @name   _GM_info demo\r\n// @namespace  Stack Overflow\r\n// @description  Tell me more about me, me, ME!\r\n// @include   http://stackoverflow.com/questions/*\r\n// @version   8.8\r\n" 
}