我如何使用tampermonkey来对本地文件进行href?

时间:2019-07-08 22:39:05

标签: javascript html css tampermonkey

我想通过href替换网站的CSS文件URL,我知道可以引用包含CSS的外部链接,但是本地文件呢?

document.querySelector("head > link:nth-child(7)").href = "http://example.com/style.css"

2 个答案:

答案 0 :(得分:0)

与所有Chrome扩展程序一样,默认情况下,Tampermonkey受到一系列旨在防止开发人员恶意行为的限制。如果Tampermonkey可以不受限制地访问您的文件系统,则用户脚本作者可以轻易滥用权限,在您不知情的情况下窃取或修改您的数据。

您最能做的是在本地计算机上托管服务器(例如,使用Node.js)。这样,您便可以为Tampermonkey提供一个keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] and shootLoop == 0: if man.left: facing = -1 else: facing = 1 if len(bullets) < 5: bullets.append( projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing)) shootLoop = 1 if keys[pygame.K_LEFT] and man.x > man.vel: man.x -= man.vel man.left = True man.right = False man.standing = False elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel: man.x += man.vel man.right = True man.left = False man.standing = False else: man.standing = True man.walkCount = 0 if not (man.isJump): if keys[pygame.K_UP]: man.isJump = True man.right = False man.left = False man.walkCount = 0 else: if man.jumpCount >= -10: neg = 1 if man.jumpCount < 0: neg = -1 man.y -= (man.jumpCount ** 2) * 0.5 * neg man.jumpCount -= 1 else: man.isJump = False man.jumpCount = 10 之类的URL,该URL可从您的计算机本地提供。不过,只有在运行服务器时,这才起作用。

或者,您可以创建自己的开发人员模式Chrome扩展程序,该扩展程序在其localhost/style.css中包含样式表,并根据web_accessible_resources规则将其插入。但是,这将完全绕开Tampermonkey,因此它无法完全回答问题。

答案 1 :(得分:-1)

一个选项是将本地文件设置为 script ,也许是将所需CSS文本分配给window属性的选项,然后可以在用户脚本中检索该属性。例如:

// ==UserScript==
// @name         local
// @match        https://example.com
// @require      file:///C:/local.js
// ==/UserScript==

document.head.appendChild(document.createElement('style'))
  .textContent = window.cssTextFromLocal;

// local.js
window.cssTextFromLocal = `
  body {
    background-color: green;
  }
`;

enter image description here

请确保permit local file access

当然,如果您想无条件执行 (无论每次加载页面如何),都无需进行脚本间通信,可以将<style>插入到local.js中的DOM。

相关问题