首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过超文本标记语言按钮动态添加新的UserScript

通过超文本标记语言按钮动态添加新的UserScript
EN

Stack Overflow用户
提问于 2021-03-02 03:06:23
回答 1查看 143关注 0票数 0

我正在做一个网站来张贴我做的一些脚本,我想在我的页面上创建一个按钮,以动态安装的UserScript与坦帕猴或Greasemonkey,就像在

Greasyfork

..。

页面示例:

脚本示例

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-02 03:43:28

您可以尝试使用隐藏的

元素使用

属性设置为以

..。您可能需要

启用某些设置

在您的浏览器中检测下载打开的UserScripts。

注意:

您可能需要将文本发送到服务器(PHP、Node等),将文件另存为

然后创建一个

代码语言:js
复制
</code> with an <code>href</code> poiting to the filename on the server to trigger a proper file download/install. When you install a script on Greasyfork, it downloads a <code>*.user.js</code> file that is hosted on their server(s). The filename that is downloaded fits the following the pattern:</p> <blockquote> <p><code>https://greasyfork.org/scripts/<ID>-script-name/code/Script%20Name.user.js</code></p> </blockquote> <p>Downloading scripts from the client or local file system is frowned upon in modern web dev.</p> <p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>const download = (content, mimeType, filename) => {   const     a = document.createElement('a'),     blob = new Blob([content], {type: mimeType}),     url = URL.createObjectURL(blob);   a.setAttribute('href', url);   a.setAttribute('download', filename);   a.click();   console.log(`Downloading: ${filename}`); };  const extractFilenameFromScript = (script) => {   const [, scriptName] = script.match(/^\/\/\s*@name\s+(.+)$/m);   return `${scriptName.replace(/[^a-z0-9]+/ig, '-').toLowerCase()}.user.js`; };  const downloadScript = () => {   const script = document.querySelector('.user-script').textContent.trim();   const filename = extractFilenameFromScript(script);   download(script, 'text/javascript', filename); };  document.querySelector('.download-btn').addEventListener('click', downloadScript);</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>.user-script {   width: 100%;   height: 8em; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code><textarea class="user-script">// ==UserScript== // @name         My Awesome Script // @namespace    com.stackoverflow.questions // @version      1.0.0 // @description  An example UserScript to download // @author       Mr. Polywhirl // @match        https://stackoverflow.com/questions/66428142 // @grant        none // ==/UserScript== (function() {     'use strict';     alert('Hello World'); })(); </textarea> <button class="download-btn">Download</button></code></pre> </div> </div> <p></p>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66428142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档