Pages - Menu

2018年1月12日 星期五

tampermonkey 筆記(二)

總是要用到下面的script
就是上一篇講沒很多的
@name
@namespace
@version
@author
@description
..這些東西
原文請參考 官網


簡單的就帶過,
困難的就略過,
普通的就掃過。XD
自己要用的其實上面打什麼並不是非常重要,
但如果要放到外面的網站上面的話,
就要打正確了。



@name : 這之程式的名字
@namespace : 這之程式的namespace,通常拿來放作者的網址
@version:版本,拿來坐版本檢查用的,關係到會不會安裝。
@author:作者
@description:簡短的描述
@homepage, @homepageURL, @website and @source:放這之程式的網頁
@icon, @iconURL and @defaulticon:icon的位置(圖片)
@icon64 and @icon64URL:icon的位置(圖片64*64pixels)
@updateURL:更新的網址,需要@version 做為更新檢查依據。
@downloadURL:當檢查到更新時的下載網址
@include:在哪些網站上可以使用這之程式,可以使用多組

// @include http://tampermonkey.net/*
// @include http://*
// @include https://*
// @include *







@match:跟@include相似 ,可用的匹配規則較多。chrome extension
@exclude:例外的網站
@require:在程式執行前預先載入的js 檔案
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @require https://code.jquery.com/jquery-2.1.3.min.js#sha256=23456...
// @require https://code.jquery.com/jquery-2.1.2.min.js#md5=34567...,sha256=6789...

@resource:預先載入GM_getResourceURL 和 GM_getResourceText可以訪問的資源
(這兩句不太懂,等以後有碰到實例再來補充)
@connect:
@run-at:
@grant:如果要使用內建的函示,需要先在上面執行把函數加載
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_setClipboard
// @grant unsafeWindow
// @grant window.close
// @grant window.focus


函數



GM_addStyle(css):加入特定的css並注入特定的元素內
GM_log(message):等同console.log
GM_openInTab(url, options):開啟新的tabe
            active:焦點放在新開的頁面
            insert:將新的頁面放在目前頁面的後面
            setParent:取代目前頁面
GM_openInTab('http://tw.yahoo.com','active');


GM_xmlhttpRequest(details):類似ajax的方式去撈資料,參考

Storage

這邊特別註明一下,下面的storage指的是 比較近代瀏覽器內建的一個存放空間,
油猴這邊講的storage,應該是放在自己的一個記憶體內。
而且這個值,會一直存在,除非關閉瀏覽器,單獨關閉頁籤資料還是會在。
GM_deleteValue(name):Deletes 'name' from storage.
GM_getValue(name, defaultValue):Get the value of 'name' from storage.
GM_setValue(name, value):Set the value of 'name' to the storage.
玩一下這段應該就知道了
// ==UserScript==
// @name         HelloWorld
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Daimom
// @match        http://www.example.com/*
// @grant        GM_getResourceURL
// @grant        GM_listValues
// @grant        GM_getValue
// @grant        GM_setValue
// @include      *
// ==/UserScript==
(function() {
     'use strict';
      var input=document.createElement("input");
     input.id="dm1";
     input.name="daimom";
     input.type="button";
     input.value="按我";
     input.onclick = showAlert;
     input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:40px;");
     document.body.appendChild(input);
})();
function showAlert(){
     var counter = GM_getValue('counter', 20);
     var count2 = GM_getValue('count2',15);
     console.log('This script has been run ' + counter + ' times.');
     console.log('This script has been run ' + count2 + ' aaatimes.');
     GM_setValue('counter', ++counter);
     console.log(GM_listValues());
}
開啟網頁:http://www.example.com/
並開啟F12工程師模式,當一直按下『按我』的按鈕,數值會一直增加,但關閉再開,資料還是在。


找到一篇全中文的,可 參考


沒有留言:

張貼留言